source: src/ASM/Assembly.ma @ 2899

Last change on this file since 2899 was 2899, checked in by sacerdot, 7 years ago
  1. some renaming ASM_xxx to OC_xxx
  2. ASM_pre_classified_system implemented (up to the same missing cases as OC_pre_classified_system) Note: the invariant that the pc is always in the program cannot be maintained in case of function pointer calls and returns. To be dropped.
File size: 58.5 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  | absolute_jump: jump_length
15  | long_jump: jump_length.
16 
17(* Functions that define the conditions under which jumps are possible *)
18definition short_jump_cond: Word → Word → (*pseudo_instruction →*)
19  bool × (BitVector 8) ≝
20  λpc_plus_jmp_length.λaddr.(*λinstr.*)
21  let 〈result, flags〉 ≝ sub_16_with_carry addr pc_plus_jmp_length false in
22  let 〈upper, lower〉 ≝ vsplit ? 9 7 result in
23    if get_index' ? 2 0 flags then
24      〈eq_bv 9 upper [[true;true;true;true;true;true;true;true;true]], true:::lower〉
25    else
26      〈eq_bv 9 upper (zero …), false:::lower〉.
27 
28definition absolute_jump_cond: Word → Word → (*pseudo_instruction →*)
29  bool × (BitVector 11) ≝
30  λpc_plus_jmp_length.λaddr.(*λinstr.*)
31  let 〈fst_5_addr, rest_addr〉 ≝ vsplit bool 5 11 addr in
32  let 〈fst_5_pc, rest_pc〉 ≝ vsplit bool 5 11 pc_plus_jmp_length in
33  〈eq_bv 5 fst_5_addr fst_5_pc, rest_addr〉.
34
35definition assembly_preinstruction ≝
36  λA: Type[0].
37  λaddr_of: A → Byte. (* relative *)
38  λpre: preinstruction A.
39  match pre with
40  [ ADD addr1 addr2 ⇒
41     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
42      [ REGISTER r ⇒ λ_.[ ([[false;false;true;false;true]]) @@ r ]
43      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;false;false;true;false;true]]); b1 ]
44      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;false;false;true;true;i1]]) ]
45      | DATA b1 ⇒ λ_. [ ([[false;false;true;false;false;true;false;false]]) ; b1 ]
46      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
47  | ADDC addr1 addr2 ⇒
48     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
49      [ REGISTER r ⇒ λ_.[ ([[false;false;true;true;true]]) @@ r ]
50      | DIRECT b1 ⇒ λ_.[ ([[false;false;true;true;false;true;false;true]]); b1 ]
51      | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;true;false;true;true;i1]]) ]
52      | DATA b1 ⇒ λ_. [ ([[false;false;true;true;false;true;false;false]]) ; b1 ]
53      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
54  | ANL addrs ⇒
55     match addrs with
56      [ inl addrs ⇒ match addrs with
57         [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
58           match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
59            [ REGISTER r ⇒ λ_.[ ([[false;true;false;true;true]]) @@ r ]
60            | DIRECT b1 ⇒ λ_.[ ([[false;true;false;true;false;true;false;true]]); b1 ]
61            | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;true;false;true;true;i1]]) ]
62            | DATA b1 ⇒ λ_. [ ([[false;true;false;true;false;true;false;false]]) ; b1 ]
63            | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
64         | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
65            let b1 ≝
66             match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
67              [ DIRECT b1 ⇒ λ_.b1
68              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
69            match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
70             [ ACC_A ⇒ λ_.[ ([[false;true;false;true;false;false;true;false]]) ; b1 ]
71             | DATA b2 ⇒ λ_. [ ([[false;true;false;true;false;false;true;true]]) ; b1 ; b2 ]
72             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
73         ]
74      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
75         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
76          [ BIT_ADDR b1 ⇒ λ_.[ ([[true;false;false;false;false;false;true;false]]) ; b1 ]
77          | N_BIT_ADDR b1 ⇒ λ_. [ ([[true;false;true;true;false;false;false;false]]) ; b1 ]
78          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
79  | CLR addr ⇒
80     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
81      [ ACC_A ⇒ λ_.
82         [ ([[true; true; true; false; false; true; false; false]]) ]
83      | CARRY ⇒ λ_.
84         [ ([[true; true; false; false; false; false; true; true]]) ]
85      | BIT_ADDR b1 ⇒ λ_.
86         [ ([[true; true; false; false; false; false; true; false]]) ; b1 ]
87      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
88  | CPL addr ⇒
89     match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with
90      [ ACC_A ⇒ λ_.
91         [ ([[true; true; true; true; false; true; false; false]]) ]
92      | CARRY ⇒ λ_.
93         [ ([[true; false; true; true; false; false; true; true]]) ]
94      | BIT_ADDR b1 ⇒ λ_.
95         [ ([[true; false; true; true; false; false; true; false]]) ; b1 ]
96      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
97  | DA addr ⇒
98     [ ([[true; true; false; true; false; true; false; false]]) ]
99  | DEC addr ⇒
100     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with
101      [ ACC_A ⇒ λ_.
102         [ ([[false; false; false; true; false; true; false; false]]) ]
103      | REGISTER r ⇒ λ_.
104         [ ([[false; false; false; true; true]]) @@ r ]
105      | DIRECT b1 ⇒ λ_.
106         [ ([[false; false; false; true; false; true; false; true]]); b1 ]
107      | INDIRECT i1 ⇒ λ_.
108         [ ([[false; false; false; true; false; true; true; i1]]) ]
109      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
110      | DJNZ addr1 addr2 ⇒
111         let b2 ≝ addr_of addr2 in
112         match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with
113          [ REGISTER r ⇒ λ_.
114             [ ([[true; true; false; true; true]]) @@ r ; b2 ]
115          | DIRECT b1 ⇒ λ_.
116             [ ([[true; true; false; true; false; true; false; true]]); b1; b2 ]
117          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
118      | JC addr ⇒
119        let b1 ≝ addr_of addr in
120          [ ([[false; true; false; false; false; false; false; false]]); b1 ]
121      | JNC addr ⇒
122         let b1 ≝ addr_of addr in
123           [ ([[false; true; false; true; false; false; false; false]]); b1 ]
124      | JZ addr ⇒
125         let b1 ≝ addr_of addr in
126           [ ([[false; true; true; false; false; false; false; false]]); b1 ]
127      | JNZ addr ⇒
128         let b1 ≝ addr_of addr in
129           [ ([[false; true; true; true; false; false; false; false]]); b1 ]
130      | JB addr1 addr2 ⇒
131         let b2 ≝ addr_of addr2 in
132         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
133          [ BIT_ADDR b1 ⇒ λ_.
134             [ ([[false; false; true; false; false; false; false; false]]); b1; b2 ]
135          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
136      | JNB addr1 addr2 ⇒
137         let b2 ≝ addr_of addr2 in
138         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
139          [ BIT_ADDR b1 ⇒ λ_.
140             [ ([[false; false; true; true; false; false; false; false]]); b1; b2 ]
141          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
142      | JBC addr1 addr2 ⇒
143         let b2 ≝ addr_of addr2 in
144         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
145          [ BIT_ADDR b1 ⇒ λ_.
146             [ ([[false; false; false; true; false; false; false; false]]); b1; b2 ]
147          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
148      | CJNE addrs addr3 ⇒
149         let b3 ≝ addr_of addr3 in
150         match addrs with
151          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
152             match addr2 return λx. bool_to_Prop (is_in ? [[direct;data]] x) → ? with
153              [ DIRECT b1 ⇒ λ_.
154                 [ ([[true; false; true; true; false; true; false; true]]); b1; b3 ]
155              | DATA b1 ⇒ λ_.
156                 [ ([[true; false; true; true; false; true; false; false]]); b1; b3 ]
157              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
158          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
159             let b2 ≝
160              match addr2 return λx. bool_to_Prop (is_in ? [[data]] x) → ? with
161               [ DATA b2 ⇒ λ_. b2
162               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) in
163             match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with
164              [ REGISTER r ⇒ λ_.
165                 [ ([[true; false; true; true; true]]) @@ r; b2; b3 ]
166              | INDIRECT i1 ⇒ λ_.
167                 [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ]
168              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)
169         ]
170  | DIV addr1 addr2 ⇒
171     [ ([[true;false;false;false;false;true;false;false]]) ]
172  | INC addr ⇒
173     match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with
174      [ ACC_A ⇒ λ_.
175         [ ([[false;false;false;false;false;true;false;false]]) ]         
176      | REGISTER r ⇒ λ_.
177         [ ([[false;false;false;false;true]]) @@ r ]
178      | DIRECT b1 ⇒ λ_.
179         [ ([[false; false; false; false; false; true; false; true]]); b1 ]
180      | INDIRECT i1 ⇒ λ_.
181        [ ([[false; false; false; false; false; true; true; i1]]) ]
182      | DPTR ⇒ λ_.
183        [ ([[true;false;true;false;false;false;true;true]]) ]
184      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
185  | JMP adptr ⇒
186     [ ([[false;true;true;true;false;false;true;true]]) ]
187  | MOV addrs ⇒
188     match addrs with
189      [ inl addrs ⇒
190         match addrs with
191          [ inl addrs ⇒
192             match addrs with
193              [ inl addrs ⇒
194                 match addrs with
195                  [ inl addrs ⇒
196                     match addrs with
197                      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
198                         match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
199                          [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ]
200                          | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ]
201                          | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ]
202                          | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ]
203                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
204                      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
205                         match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with
206                          [ REGISTER r ⇒ λ_.
207                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
208                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ]
209                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ]
210                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ]
211                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
212                          | INDIRECT i1 ⇒ λ_.
213                             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with
214                              [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ]
215                              | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ]
216                              | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ]
217                              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
218                          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
219                  | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
220                     let b1 ≝
221                      match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
222                       [ DIRECT b1 ⇒ λ_. b1
223                       | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
224                     match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with
225                      [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1]
226                      | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ]
227                      | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ]
228                      | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ]
229                      | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ]
230                      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
231              | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
232                 match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with
233                  [ DATA16 w ⇒ λ_.
234                     let b1_b2 ≝ vsplit ? 8 8 w in
235                     let b1 ≝ \fst b1_b2 in
236                     let b2 ≝ \snd b1_b2 in
237                      [ ([[true;false;false;true;false;false;false;false]]); b1; b2]
238                  | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
239          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
240             match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
241              [ BIT_ADDR b1 ⇒ λ_.
242                 [ ([[true;false;true;false;false;false;true;false]]); b1 ]
243              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
244      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
245         match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with
246          [ BIT_ADDR b1 ⇒ λ_.
247             [ ([[true;false;false;true;false;false;true;false]]); b1 ]
248          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
249  | MOVX addrs ⇒
250     match addrs with
251      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
252         match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
253          [ EXT_INDIRECT i1 ⇒ λ_.
254             [ ([[true;true;true;false;false;false;true;i1]]) ]
255          | EXT_INDIRECT_DPTR ⇒ λ_.
256             [ ([[true;true;true;false;false;false;false;false]]) ]
257          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
258      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
259         match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with
260          [ EXT_INDIRECT i1 ⇒ λ_.
261             [ ([[true;true;true;true;false;false;true;i1]]) ]
262          | EXT_INDIRECT_DPTR ⇒ λ_.
263             [ ([[true;true;true;true;false;false;false;false]]) ]
264          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)]
265  | MUL addr1 addr2 ⇒
266     [ ([[true;false;true;false;false;true;false;false]]) ]
267  | NOP ⇒
268     [ ([[false;false;false;false;false;false;false;false]]) ]
269  | ORL addrs ⇒
270     match addrs with
271      [ inl addrs ⇒
272         match addrs with
273          [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
274             match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with
275             [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ]
276             | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ]
277             | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ]
278             | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ]
279             | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
280          | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
281            let b1 ≝
282              match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
283               [ DIRECT b1 ⇒ λ_. b1
284               | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
285             match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
286              [ ACC_A ⇒ λ_.
287                 [ ([[false;true;false;false;false;false;true;false]]); b1 ]
288              | DATA b2 ⇒ λ_.
289                 [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ]
290              | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
291      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in     
292         match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with
293          [ BIT_ADDR b1 ⇒ λ_.
294             [ ([[false;true;true;true;false;false;true;false]]); b1 ]
295          | N_BIT_ADDR b1 ⇒ λ_.
296             [ ([[true;false;true;false;false;false;false;false]]); b1 ]
297          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
298  | POP addr ⇒
299     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
300      [ DIRECT b1 ⇒ λ_.
301         [ ([[true;true;false;true;false;false;false;false]]) ; b1 ]
302      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
303  | PUSH addr ⇒
304     match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
305      [ DIRECT b1 ⇒ λ_.
306         [ ([[true;true;false;false;false;false;false;false]]) ; b1 ]
307      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
308  | RET ⇒
309     [ ([[false;false;true;false;false;false;true;false]]) ]
310  | RETI ⇒
311     [ ([[false;false;true;true;false;false;true;false]]) ]
312  | RL addr ⇒
313     [ ([[false;false;true;false;false;false;true;true]]) ]
314  | RLC addr ⇒
315     [ ([[false;false;true;true;false;false;true;true]]) ]
316  | RR addr ⇒
317     [ ([[false;false;false;false;false;false;true;true]]) ]
318  | RRC addr ⇒
319     [ ([[false;false;false;true;false;false;true;true]]) ]
320  | SETB addr ⇒     
321     match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with
322      [ CARRY ⇒ λ_.
323         [ ([[true;true;false;true;false;false;true;true]]) ]
324      | BIT_ADDR b1 ⇒ λ_.
325         [ ([[true;true;false;true;false;false;true;false]]); b1 ]
326      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
327  | SUBB addr1 addr2 ⇒
328     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with
329      [ REGISTER r ⇒ λ_.
330         [ ([[true;false;false;true;true]]) @@ r ]
331      | DIRECT b1 ⇒ λ_.
332         [ ([[true;false;false;true;false;true;false;true]]); b1]
333      | INDIRECT i1 ⇒ λ_.
334         [ ([[true;false;false;true;false;true;true;i1]]) ]
335      | DATA b1 ⇒ λ_.
336         [ ([[true;false;false;true;false;true;false;false]]); b1]
337      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
338  | SWAP addr ⇒
339     [ ([[true;true;false;false;false;true;false;false]]) ]
340  | XCH addr1 addr2 ⇒
341     match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with
342      [ REGISTER r ⇒ λ_.
343         [ ([[true;true;false;false;true]]) @@ r ]
344      | DIRECT b1 ⇒ λ_.
345         [ ([[true;true;false;false;false;true;false;true]]); b1]
346      | INDIRECT i1 ⇒ λ_.
347         [ ([[true;true;false;false;false;true;true;i1]]) ]
348      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
349  | XCHD addr1 addr2 ⇒
350     match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with
351      [ INDIRECT i1 ⇒ λ_.
352         [ ([[true;true;false;true;false;true;true;i1]]) ]
353      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
354  | XRL addrs ⇒
355     match addrs with
356      [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
357         match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with
358          [ REGISTER r ⇒ λ_.
359             [ ([[false;true;true;false;true]]) @@ r ]
360          | DIRECT b1 ⇒ λ_.
361             [ ([[false;true;true;false;false;true;false;true]]); b1]
362          | INDIRECT i1 ⇒ λ_.
363             [ ([[false;true;true;false;false;true;true;i1]]) ]
364          | DATA b1 ⇒ λ_.
365             [ ([[false;true;true;false;false;true;false;false]]); b1]
366          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
367      | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in
368         let b1 ≝
369          match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with
370           [ DIRECT b1 ⇒ λ_. b1
371           | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in
372         match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with
373          [ ACC_A ⇒ λ_.
374             [ ([[false;true;true;false;false;false;true;false]]); b1 ]         
375          | DATA b2 ⇒ λ_.
376             [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ]
377          | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)]
378       ].
379
380definition assembly1 ≝
381 λi: instruction.
382 match i with
383  [ ACALL addr ⇒
384     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
385      [ ADDR11 w ⇒ λ_.
386         let v1_v2 ≝ vsplit ? 3 8 w in
387         let v1 ≝ \fst v1_v2 in
388         let v2 ≝ \snd v1_v2 in
389          [ (v1 @@ [[true; false; false; false; true]]) ; v2 ]
390      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
391  | AJMP addr ⇒
392     match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with
393      [ ADDR11 w ⇒ λ_.
394         let v1_v2 ≝ vsplit ? 3 8 w in
395         let v1 ≝ \fst v1_v2 in
396         let v2 ≝ \snd v1_v2 in
397          [ (v1 @@ [[false; false; false; false; true]]) ; v2 ]
398      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
399  | LCALL addr ⇒
400     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
401      [ ADDR16 w ⇒ λ_.
402         let b1_b2 ≝ vsplit ? 8 8 w in
403         let b1 ≝ \fst b1_b2 in
404         let b2 ≝ \snd b1_b2 in
405          [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ]         
406      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
407  | LJMP addr ⇒
408     match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with
409      [ ADDR16 w ⇒ λ_.
410         let b1_b2 ≝ vsplit ? 8 8 w in
411         let b1 ≝ \fst b1_b2 in
412         let b2 ≝ \snd b1_b2 in
413          [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ]         
414      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
415  | MOVC addr1 addr2 ⇒
416     match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with
417      [ ACC_DPTR ⇒ λ_.
418         [ ([[true;false;false;true;false;false;true;true]]) ]
419      | ACC_PC ⇒ λ_.
420         [ ([[true;false;false;false;false;false;true;true]]) ]
421      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)
422  | SJMP addr ⇒
423     match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with
424      [ RELATIVE b1 ⇒ λ_.
425         [ ([[true;false;false;false;false;false;false;false]]); b1 ]
426      | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr)
427  | RealInstruction instr ⇒
428    assembly_preinstruction [[ relative ]]
429      (λx.
430        match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with
431        [ RELATIVE r ⇒ λ_. r
432        | _ ⇒ λabsd. ⊥
433        ] (subaddressing_modein … x)) instr
434  ].
435  cases absd
436qed.
437
438(* XXX: pc_plus_sjmp_length used to be just sigma of ppc.  This is incorrect
439        as relative lengths are computed from the *end* of the SJMP, not from
440        the beginning.
441*)
442definition expand_relative_jump_internal:
443 ∀lookup_labels:Identifier → Word.∀sigma:Word → Word.∀policy:Word → bool.
444 Identifier → Word → ([[relative]] → preinstruction [[relative]]) →
445 list instruction
446 ≝
447  λlookup_labels.λsigma.λpolicy.λlbl.λppc,i.
448   let lookup_address ≝ sigma (lookup_labels lbl) in
449   let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
450   let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
451   if sj_possible ∧ ¬(policy ppc)
452   then
453     let address ≝ RELATIVE disp in
454       [ RealInstruction (i address) ]
455   else
456    [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2)));
457      SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *)
458      LJMP (ADDR16 lookup_address)
459    ].
460  %
461qed.
462
463definition expand_relative_jump:
464  ∀lookup_labels.∀sigma.∀policy.
465  Word → (*jump_length →*)
466  preinstruction Identifier → list instruction ≝
467  λlookup_labels: Identifier → Word.
468  λsigma:Word → Word.
469  λpolicy:Word → bool.
470  λppc: Word.
471  (*λjmp_len: jump_length.*)
472  λi: preinstruction Identifier.
473  (*let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in*)
474  match i with
475  [ JC jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JC ?)
476  | JNC jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JNC ?)
477  | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JB ? baddr)
478  | JZ jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JZ ?)
479  | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JNZ ?)
480  | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JBC ? baddr)
481  | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (JNB ? baddr)
482  | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (CJNE ? addr)
483  | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels sigma policy jmp ppc (DJNZ ? addr)
484  | ADD arg1 arg2 ⇒ [ ADD ? arg1 arg2 ]
485  | ADDC arg1 arg2 ⇒ [ ADDC ? arg1 arg2 ]
486  | SUBB arg1 arg2 ⇒ [ SUBB ? arg1 arg2 ]
487  | INC arg ⇒ [ INC ? arg ]
488  | DEC arg ⇒ [ DEC ? arg ]
489  | MUL arg1 arg2 ⇒ [ MUL ? arg1 arg2 ]
490  | DIV arg1 arg2 ⇒ [ DIV ? arg1 arg2 ]
491  | DA arg ⇒ [ DA ? arg ]
492  | ANL arg ⇒ [ ANL ? arg ]
493  | ORL arg ⇒ [ ORL ? arg ]
494  | XRL arg ⇒ [ XRL ? arg ]
495  | CLR arg ⇒ [ CLR ? arg ]
496  | CPL arg ⇒ [ CPL ? arg ]
497  | RL arg ⇒ [ RL ? arg ]
498  | RR arg ⇒ [ RR ? arg ]
499  | RLC arg ⇒ [ RLC ? arg ]
500  | RRC arg ⇒ [ RRC ? arg ]
501  | SWAP arg ⇒ [ SWAP ? arg ]
502  | MOV arg ⇒ [ MOV ? arg ]
503  | MOVX arg ⇒ [ MOVX ? arg ]
504  | SETB arg ⇒ [ SETB ? arg ]
505  | PUSH arg ⇒ [ PUSH ? arg ]
506  | POP arg ⇒ [ POP ? arg ]
507  | XCH arg1 arg2 ⇒ [ XCH ? arg1 arg2 ]
508  | XCHD arg1 arg2 ⇒ [ XCHD ? arg1 arg2 ]
509  | RET ⇒ [ RET ? ]
510  | RETI ⇒ [ RETI ? ]
511  | NOP ⇒ [ NOP ? ]
512  | JMP arg ⇒ [ RealInstruction (JMP ? arg) ]
513  ].
514
515definition expand_pseudo_instruction:
516    ∀lookup_labels.
517    ∀sigma: Word → Word.
518    ∀policy: Word → bool.
519      Word → ? → pseudo_instruction → list instruction ≝
520  λlookup_labels: Identifier → Word.
521  λsigma: Word → Word.
522  λpolicy: Word → bool.
523  λppc.
524  λlookup_datalabels:Identifier → Word.
525  λi.
526  match i with
527  [ Cost cost ⇒ [ ]
528  | Comment comment ⇒ [ ]
529  | Call call ⇒
530    let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
531    let lookup_address ≝ sigma (lookup_labels call) in
532    let 〈mj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
533    let do_a_long ≝ policy ppc in
534    if mj_possible ∧ ¬ do_a_long then
535      let address ≝ ADDR11 disp in
536        [ ACALL address ]
537    else
538      let address ≝ ADDR16 lookup_address in
539        [ LCALL address ]
540  | Mov d trgt ⇒
541    let address ≝ DATA16 (lookup_datalabels trgt) in
542      [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))]
543  | Instruction instr ⇒ expand_relative_jump lookup_labels sigma policy ppc instr
544  | Jmp jmp ⇒
545    let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
546    let do_a_long ≝ policy ppc in
547    let lookup_address ≝ sigma (lookup_labels jmp) in
548    let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
549    if sj_possible ∧ ¬ do_a_long then
550      let address ≝ RELATIVE disp in
551        [ SJMP address ]
552    else
553      let 〈mj_possible, disp2〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
554      if mj_possible ∧ ¬ do_a_long then
555        let address ≝ ADDR11 disp2 in
556          [ AJMP address ]
557      else   
558        let address ≝ ADDR16 lookup_address in
559        [ LJMP address ]
560  | Jnz acc tgt1 tgt2 ⇒
561     let lookup_address1 ≝ sigma (lookup_labels tgt1) in
562     let lookup_address2 ≝ sigma (lookup_labels tgt2) in
563      (*CSC: we inefficiently use always LJMPs; the policy could
564        choose two SJMPs instead *)
565      [ RealInstruction (JNZ … (RELATIVE (bitvector_of_nat ? 3)));
566        LJMP (ADDR16 lookup_address2);
567        LJMP (ADDR16 lookup_address1) ]
568  | MovSuccessor dst ws lbl ⇒
569     let addr ≝ lookup_labels lbl in
570     let 〈high, low〉 ≝ vsplit ? 8 8 addr in
571     let v ≝ DATA match ws with [ HIGH ⇒ high | LOW ⇒ low ] in
572     match dst return λx. bool_to_Prop (is_in ? [[acc_a;direct;registr]] x) → ? with
573     [ ACC_A ⇒ λ_.
574        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? 〈ACC_A, v〉))))))]     
575     | DIRECT b1 ⇒ λ_.
576        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inr ? ? 〈DIRECT b1, v〉)))))]
577     | REGISTER r ⇒ λ_.
578        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? (inr ? ? 〈REGISTER r, v〉))))))]     | _ ⇒ λK. match K in False with [ ] ] (subaddressing_modein … dst)
579  ].
580  try %
581qed.
582 
583definition assembly_1_pseudoinstruction ≝
584  λlookup_labels.
585  λsigma: Word → Word.
586  λpolicy: Word → bool.
587  λppc: Word.
588  λlookup_datalabels.
589  λi.
590  let pseudos ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels i in
591  let mapped ≝ map ? ? assembly1 pseudos in
592  let flattened ≝ flatten ? mapped in
593  let pc_len ≝ length ? flattened in
594   〈pc_len, flattened〉.
595
596definition instruction_size ≝
597  λlookup_labels.
598  λlookup_datalabels.
599  λsigma: Word → Word.
600  λpolicy: Word → bool.
601  λppc.
602  λi.
603    \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels i).
604 
605(* Labels *)
606definition is_label ≝
607  λx:labelled_instruction.λl:Identifier.
608  let 〈lbl,instr〉 ≝ x in
609  match lbl with
610  [ Some l' ⇒ l' = l
611  | _       ⇒ False
612  ].
613
614lemma label_does_not_occur:
615  ∀i:ℕ.∀p:list labelled_instruction.∀l:Identifier.
616  is_label (nth i ? p 〈None ?, Comment EmptyString〉) l → does_not_occur ?? l p = false.
617 #i #p #l generalize in match i; elim p
618 [ #i >nth_nil #H cases H
619 | #h #t #IH #i cases i -i
620   [ cases h #hi #hp cases hi
621     [ normalize #H cases H
622     | #l' #Heq whd in ⊢ (??%?); change with (eq_identifier ? l' l) in match (instruction_matches_identifier ????);
623       whd in Heq; >Heq
624       >eq_identifier_refl / by refl/
625     ]
626   | #i #H whd in match (does_not_occur ????);
627     whd in match (instruction_matches_identifier ????);
628     cases h #hi #hp cases hi normalize nodelta
629     [ @(IH i) @H
630     | #l' @eq_identifier_elim
631       [ normalize / by /
632       | normalize #_ @(IH i) @H
633       ]
634     ]
635   ]
636 ]
637qed.
638
639definition sigma_policy_specification ≝
640  λprogram: pseudo_assembly_program.
641  λsigma: Word → Word.
642  λpolicy: Word → bool.
643  sigma (zero …) = zero … ∧
644  let instr_list ≝ code program in
645  let preamble ≝ preamble program in
646  ∀ppc: Word. ∀ppc_ok: nat_of_bitvector … ppc < |instr_list|.
647    let pc ≝ sigma ppc in
648    let labels ≝ \fst (create_label_cost_map instr_list) in
649    let lookup_labels ≝ λx. bitvector_of_nat 16 (lookup_def … labels x 0) in
650    let lookup_datalabels ≝ λx. lookup_def … (construct_datalabels preamble) x (zero …) in
651    let instruction ≝ \fst (fetch_pseudo_instruction instr_list ppc ppc_ok) in
652    let next_pc ≝ sigma (add 16 ppc (bitvector_of_nat 16 1)) in
653     next_pc = add 16 pc (bitvector_of_nat … (instruction_size lookup_labels lookup_datalabels sigma policy ppc instruction))
654     ∧
655     (nat_of_bitvector … pc + instruction_size lookup_labels lookup_datalabels sigma policy ppc instruction < 2^16
656     ∨
657     (∀ppc'. ∀ppc_ok':nat_of_bitvector … ppc' < |instr_list|. nat_of_bitvector … ppc < nat_of_bitvector … ppc' →
658       let instruction' ≝ \fst (fetch_pseudo_instruction instr_list ppc' ppc_ok') in
659       instruction_size lookup_labels lookup_datalabels sigma policy ppc' instruction' = 0)
660     ∧
661     nat_of_bitvector … pc + instruction_size lookup_labels lookup_datalabels sigma policy ppc instruction = 2^16).
662
663
664lemma fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels:
665 ∀lookup_labels,sigma,policy,ppc,pi.
666  ∀lookup_datalabels1,lookup_datalabels2.
667   \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels1 pi) =
668   \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels2 pi).
669#lookup_labels #sigma #policy #ppc #pi #lookup_datalabels1 #lookup_datalabels2
670cases pi //
671qed.
672
673lemma fst_snd_assembly_1_pseudoinstruction:
674 ∀lookup_labels,sigma,policy,ppc,pi,lookup_datalabels,len,assembled.
675   assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi
676   = 〈len,assembled〉 →
677    len = |assembled|.
678#lookup #sigma #policy #ppc #pi #lookup_datalabels #len #assembled
679inversion (assembly_1_pseudoinstruction ??????) #len' #assembled'
680whd in ⊢ (??%? → ?); #EQ1 #EQ2 destruct %
681qed.
682
683(* XXX: easy but tedious *)
684lemma assembly1_lt_128:
685  ∀i: instruction.
686    |(assembly1 i)| < 128.
687  cases daemon
688(* XXX: commented out as takes ages to type check
689  #i cases i
690  try (#assm1 #assm2) try #assm1
691  [8:
692    cases assm1
693    try (#assm1 #assm2) try #assm1
694    whd in match assembly1; normalize nodelta
695    whd in match assembly_preinstruction; normalize nodelta
696    try @(subaddressing_mode_elim … assm2)
697    try @(subaddressing_mode_elim … assm1) try #w try #w' normalize nodelta
698    [32:
699      cases assm1 -assm1 #assm1 normalize nodelta
700      cases assm1 #addr1 #addr2 normalize nodelta
701      [1:
702        @(subaddressing_mode_elim … addr2)
703      |2:
704        @(subaddressing_mode_elim … addr1)
705      ]
706      #w
707    |35,36,37:
708      cases assm1 -assm1 #assm1 normalize nodelta
709      [1,3:
710        cases assm1 -assm1 #assm1 normalize nodelta
711      ]
712      cases assm1 #addr1 #addr2 normalize nodelta
713      @(subaddressing_mode_elim … addr2) try #w
714    |49:
715      cases assm1 -assm1 #assm1 normalize nodelta
716      [1:
717        cases assm1 -assm1 #assm1 normalize nodelta
718        [1:
719          cases assm1 -assm1 #assm1 normalize nodelta
720          [1:
721            cases assm1 -assm1 #assm1 normalize nodelta
722            [1:
723              cases assm1 -assm1 #assm1 normalize nodelta
724            ]
725          ]
726        ]
727      ]
728      cases assm1 #addr1 #addr2 normalize nodelta
729      [1,3,4,5:
730        @(subaddressing_mode_elim … addr2) try #w
731      |*:
732        @(subaddressing_mode_elim … addr1) try #w
733        normalize nodelta
734        [1,2:
735          @(subaddressing_mode_elim … addr2) try #w
736        ]
737      ]
738    |50:
739      cases assm1 -assm1 #assm1 normalize nodelta
740      cases assm1 #addr1 #addr2 normalize nodelta
741      [1:
742        @(subaddressing_mode_elim … addr2) try #w
743      |2:
744        @(subaddressing_mode_elim … addr1) try #w
745      ]
746    ]
747    normalize repeat @le_S_S @le_O_n
748  ]
749  whd in match assembly1; normalize nodelta
750  [6:
751    normalize repeat @le_S_S @le_O_n
752  |7:
753    @(subaddressing_mode_elim … assm2) normalize repeat @le_S_S @le_O_n
754  |*:
755    @(subaddressing_mode_elim … assm1) #w normalize nodelta repeat @le_S_S @le_O_n
756  ]
757  *)
758qed.
759
760lemma assembly1_pseudoinstruction_lt_2_to_16:
761  ∀lookup_labels,sigma,policy,ppc,lookup_datalabels,pi.
762  |\snd (assembly_1_pseudoinstruction
763    lookup_labels sigma policy ppc lookup_datalabels pi)|
764   < 2^16.
765 #lookup_labels #sigma #policy #ppc #lookup_datalabels *
766[ cut (128 < 2^16) [@leb_true_to_le %] #LT
767  * whd in match (assembly_1_pseudoinstruction ??????);
768  whd in match (expand_pseudo_instruction ??????);
769  whd in match assembly_1_pseudoinstruction; normalize nodelta
770  try (#arg1 #arg2 #arg3) try (#arg1 #arg2) try #arg1
771  whd in match (expand_pseudo_instruction ??????);
772  try
773   (change with (|flatten ? [assembly1 ?]| < ?)
774    >flatten_singleton
775    @(transitive_lt … (assembly1_lt_128 ?))
776    @LT)
777  @pair_elim #x #y #_ cases x cases (policy ppc) normalize nodelta
778  try
779   (change with (|flatten ? [assembly1 ?]| < ?)
780    >flatten_singleton
781    @(transitive_lt … (assembly1_lt_128 ?))
782    @LT)
783  change with (|flatten ? [assembly1 ?; assembly1 ?; assembly1 ?]| < ?)
784  >length_flatten_cons >length_flatten_cons >length_flatten_cons <plus_n_O
785  <associative_plus @(transitive_lt … (tech_transitive_lt_3 … (2^7) ???))
786  try @assembly1_lt_128 @leb_true_to_le %
787|2,3: #msg normalize in ⊢ (?%?); //
788| #label whd in match (assembly_1_pseudoinstruction ??????);
789  whd in match (expand_pseudo_instruction ??????);
790  @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_
791  [2: @pair_elim #x #y #_ cases (?∧?)]
792  normalize in ⊢ (?%?); //
793|7: #label whd in match (assembly_1_pseudoinstruction ??????);
794  whd in match (expand_pseudo_instruction ??????);
795  @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_
796  normalize in ⊢ (?%?); //
797|8: #dptr #id normalize in ⊢ (?%?); //
798|5: #acc #dst1 #dst2 normalize in ⊢ (?%?); //
799|6: #dst #ws #lbl whd in match (assembly_1_pseudoinstruction ??????);
800    whd in match (expand_pseudo_instruction ??????);
801    @pair_elim #high #low #EQ @(subaddressing_mode_elim … dst)
802    normalize nodelta [2,3: #w] normalize in ⊢ (?%?); //
803]
804qed.
805
806definition assembly:
807    ∀p: pseudo_assembly_program.
808    ∀sigma: Word → Word.
809    ∀policy: Word → bool.
810      Σres:labelled_object_code.
811       sigma_policy_specification p sigma policy →
812       let preamble ≝ preamble p in
813       let instr_list ≝ code p in
814       |instr_list| ≤ 2^16 →
815       let assembled ≝ oc res in
816       |assembled| ≤ 2^16 ∧
817       (nat_of_bitvector … (sigma (bitvector_of_nat … (|instr_list|))) = |assembled| ∨
818        sigma (bitvector_of_nat … (|instr_list|)) = zero … ∧ |assembled| = 2^16) ∧
819       let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in
820       let datalabels ≝ construct_datalabels preamble in
821       let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in
822       let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
823       ∀ppc. ∀ppc_ok:nat_of_bitvector … ppc < |instr_list|.
824         let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc ppc_ok in
825         let 〈len,assembledi〉 ≝
826          assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in
827         |assembledi| ≤ |assembled| ∧
828         ∀j:nat. ∀H: j < |assembledi|. ∃K.
829          nth_safe ? j assembledi H =
830           nth_safe ? (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat ? j)))
831            assembled K
832
833  λp.
834  λsigma.
835  λpolicy.
836  deplet 〈labels_to_ppc,ppc_to_costs〉 as eq_create_label_cost_map ≝ create_label_cost_map (code p) in
837  let preamble ≝ preamble p in
838  let instr_list ≝ code p in
839  let datalabels ≝ construct_datalabels preamble in
840  let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in
841  let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in
842  let 〈next_pc,revcode〉 ≝ pi1 … (
843     foldl_strong
844      (option Identifier × pseudo_instruction)
845      (λpre. Σppc_code:(Word × (list Byte)).
846       sigma_policy_specification p sigma policy →
847        |instr_list| ≤ 2^16 →
848        let 〈ppc,code〉 ≝ ppc_code in
849         ppc = bitvector_of_nat … (|pre|) ∧
850         |code| ≤ 2^16 ∧
851         (nat_of_bitvector … (sigma ppc) = |code| ∨
852          sigma ppc = zero … ∧ |code| = 2^16 ∧
853          (|pre| < 2^16 → ∀ppc'. ∀ppc_ok':nat_of_bitvector … ppc' < |instr_list|. nat_of_bitvector … ppc ≤ nat_of_bitvector … ppc' →
854            let instruction' ≝ \fst (fetch_pseudo_instruction instr_list ppc' ppc_ok') in
855            instruction_size lookup_labels lookup_datalabels sigma policy ppc' instruction' = 0)
856         ) ∧
857         ∀ppc'.∀ppc_ok'.
858          (nat_of_bitvector … ppc' < nat_of_bitvector … ppc ∨ |pre| = 2^16) →
859           let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' ppc_ok' in
860           let 〈len,assembledi〉 ≝
861            assembly_1_pseudoinstruction lookup_labels sigma policy ppc' lookup_datalabels pi in
862           |assembledi| ≤ |reverse … code| ∧
863           ∀j:nat. ∀H: j < |assembledi|. ∃K.
864            nth_safe ? j assembledi H =
865             nth_safe ? (nat_of_bitvector … (add … (sigma ppc') (bitvector_of_nat ? j))) (reverse … code) K)
866      instr_list
867      (λprefix,hd,tl,prf,ppc_code.
868        let 〈ppc, code〉 ≝ pi1 … ppc_code in
869        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels (\snd hd) in
870        let new_ppc ≝ add ? ppc (bitvector_of_nat ? 1) in
871         〈new_ppc, (reverse … program @ code)〉)
872      〈(zero ?), [ ]〉)
873    in
874     mk_labelled_object_code
875     (reverse … revcode)
876     (fold … (λppc.λcost.λpc_to_costs. insert … (sigma ppc) cost pc_to_costs) ppc_to_costs (Stub ??))
877     (foldl ??
878      (λsymboltable,newident_oldident.
879        let ppc ≝ lookup_labels (\fst newident_oldident) in
880         insert … (sigma ppc) (\snd newident_oldident) symboltable) (Stub ??) (renamed_symbols p))
881     (sigma (lookup_labels (final_label p))) ?.
882  [ cases (foldl_strong ? (λx.Σy.?) ???) in p1; #ignore_revcode #Hfold #EQignore_revcode
883    >EQignore_revcode in Hfold; #Hfold #sigma_pol_ok #instr_list_ok
884    cases (Hfold sigma_pol_ok instr_list_ok) -Hfold * * #Hfold1 #Hfold4 #Hfold5 #Hfold3 whd
885    <eq_create_label_cost_map whd %
886    [2: #ppc #LTppc @Hfold3 >Hfold1 @(eqb_elim (|instr_list|) 2^16)
887      [ #limit %2 @limit
888      | #nlimit %1 >nat_of_bitvector_bitvector_of_nat_inverse try assumption
889        @not_eq_to_le_to_lt assumption ]
890    | >length_reverse % try assumption cases Hfold5 -Hfold5
891      [ #Hfold5 <Hfold5 % >Hfold1 %
892      | * #Hfold51 #Hfold52 %2 <Hfold1 assumption ]]
893  | cases daemon
894  | * #sigma_pol_ok1 #_ #instr_list_ok %
895    [ % % [%] // >sigma_pol_ok1 % ]
896    #ppc' #ppc_ok' #abs @⊥ cases abs
897     [#abs2 cases (not_le_Sn_O ?) [#H @(H abs2) | skip]
898     |#abs2 change with (0 = S ?) in abs2; destruct(abs2) ]
899  | #sigma_pol_ok cases sigma_pol_ok #sigma_pol_ok1 #sigma_pol_ok2 #instr_list_ok cases ppc_code in p1; -ppc_code #ppc_code #IH #EQppc_code >EQppc_code in IH; -EQppc_code
900    #IH cases (IH ? instr_list_ok) [2: % assumption ] -IH
901    * * #IH1 #IH2 #IH3 #IH4
902    cut (|prefix| < |instr_list|)
903    [ >prf >length_append normalize <plus_n_Sm @le_S_S // ] #LT_prefix_instr_list
904    cut (|prefix| < 2^16)
905    [ @(lt_to_le_to_lt … (|instr_list|)) assumption ] #prefix_ok
906    cut (nat_of_bitvector … ppc < |instr_list|)
907    [ >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ] #ppc_ok
908    cut (\snd hd = \fst (fetch_pseudo_instruction instr_list ppc ppc_ok))
909    [ >prf in ppc_ok; >IH1 >(add_zero … (bitvector_of_nat … (|prefix|)))
910      >fetch_pseudo_instruction_append
911      [ #ppc_ok whd in match fetch_pseudo_instruction; normalize nodelta
912        whd in match (nth_safe ????); [ cases hd // | normalize // ]
913      | <add_zero >nat_of_bitvector_bitvector_of_nat_inverse
914        [ <prf assumption | assumption ]
915      | skip
916      | <prf assumption
917      ]] #eq_fetch_pseudo_instruction
918    lapply (fst_snd_assembly_1_pseudoinstruction … p2) #EQpc_delta
919    cut (pc_delta < 2^16)
920    [ >EQpc_delta
921      @(eq_ind ?? (λp.λ_. |\snd p| < 2^16) ?? p2)
922      @assembly1_pseudoinstruction_lt_2_to_16 ] #pc_delta_ok
923    cut (pc_delta = instruction_size lookup_labels lookup_datalabels sigma policy ppc (\snd hd))
924    [ whd in match instruction_size; normalize nodelta
925      >fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels [ >p2 | skip] % ]
926    #EQpc_delta2
927    cases (sigma_pol_ok2 … ppc_ok)
928    <eq_fetch_pseudo_instruction <eq_create_label_cost_map <EQpc_delta2
929    #sigma_pol3 #sigma_pol4
930    % [ % [% ] ]
931    [ >length_append normalize nodelta >IH1 @sym_eq @add_bitvector_of_nat
932    | >length_append >length_reverse <EQpc_delta
933      cases IH3 -IH3
934      [ #IH3 <IH3 >commutative_plus
935        cases sigma_pol4 [ #LT @(transitive_le … LT) // | * #_ #EQ >EQ % ]
936      | * * #IH3a #IH3b #IH3c >IH3b <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction
937        >IH3c try % assumption ]
938    | >length_append >length_reverse
939      cases IH3 -IH3
940      [ #IH3 <IH3 <EQpc_delta cases sigma_pol4
941        [ #LT %1 >sigma_pol3 >nat_of_bitvector_add
942          [2: >nat_of_bitvector_bitvector_of_nat_inverse assumption]
943          >nat_of_bitvector_bitvector_of_nat_inverse try assumption //
944        | * #EQ1 #EQ2 %2 %
945          [ lapply (eq_f … (bitvector_of_nat 16) … EQ2) <add_bitvector_of_nat_plus
946            >bitvector_of_nat_inverse_nat_of_bitvector <sigma_pol3 #X >X % //
947          | #LLT_prefix
948            cut (S (nat_of_bitvector … ppc) < 2^16)
949            [ >length_append in LLT_prefix; <plus_n_Sm <plus_n_O #LLT_prefix
950              >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ]
951            -LLT_prefix #LLT_prefix
952            #ppc' #ppc_ok' #LEQ_newppc_ppc' whd >EQ1 try %
953            @(lt_to_le_to_lt … LEQ_newppc_ppc') normalize nodelta
954            >nat_of_bitvector_add >nat_of_bitvector_bitvector_of_nat_inverse // ]]
955      | * * #IH5 #IH6 #IH7 %2 % [% ]
956        [ normalize nodelta >sigma_pol3 >IH5
957          >add_commutative <add_zero >nat_of_bitvector_bitvector_of_nat_inverse try assumption
958          >EQpc_delta2 >eq_fetch_pseudo_instruction >IH7 try % assumption
959        | >IH6 <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction >IH7 try %
960          assumption
961        | #LLT_prefix
962          cut (S (nat_of_bitvector … ppc) < 2^16)
963          [ >length_append in LLT_prefix; <plus_n_Sm <plus_n_O #LLT_prefix
964            >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ]
965          -LLT_prefix #LLT_prefix
966          #ppc' #ppc_ok' #LEQ_newppc_ppc' whd @IH7 try assumption
967          @(transitive_le … LEQ_newppc_ppc') normalize nodelta
968          >nat_of_bitvector_add >nat_of_bitvector_bitvector_of_nat_inverse // ]]
969  | #ppc' #LTppc' cases hd in prf p2 EQpc_delta2 eq_fetch_pseudo_instruction; #label #pi #prf #p2
970    #EQpc_delta2 #eq_fetch_pseudo_instruction #OR_lt_eq @(eq_bv_elim … ppc' ppc)
971    [ #EQppc' >EQppc' in LTppc'; -ppc' >prf
972      >IH1 #LTppc lapply LTppc
973      >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → match % with [_ ⇒ ?]);
974      >fetch_pseudo_instruction_append
975      [3: @le_S_S @le_O_n
976      |2: lapply LTppc; >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → ?); #H @H
977      |4: <prf @instr_list_ok ]
978      #LTppc' @pair_elim #pi' #newppc' #EQpair destruct(EQpair) <IH1 >p2 %
979      [ >length_reverse >length_append >length_reverse // ]
980      #j #LTj >nat_of_bitvector_add
981      >nat_of_bitvector_bitvector_of_nat_inverse
982      [2,4: @(lt_to_le_to_lt … LTj) <EQpc_delta @(transitive_le … pc_delta_ok) %2 %
983      |3: @(lt_to_le_to_lt … (nat_of_bitvector … (sigma ppc) + pc_delta))
984          [ >EQpc_delta @monotonic_lt_plus_r assumption
985          | cases sigma_pol4
986            [ #H @(transitive_le … H) %2 %
987            | * #_ #EQ >EQ % ]]]
988      >reverse_append >reverse_reverse
989      cases IH3 -IH3
990      [ #IH3 >IH3 <(length_reverse … code) %
991        [ >length_append @monotonic_lt_plus_r assumption
992        | @nth_safe_prepend ]
993      | * * #IH3a #IH3b #IH3c >IH3a @⊥
994        cut (|program| = 0)
995        [ <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction @IH3c // ] #EQprogram
996        @(absurd ?? (not_le_Sn_O j)) <EQprogram assumption ]
997    | #NEQppc'
998      lapply (IH4 … LTppc')
999      @pair_elim #pi' #newppc' #eq_fetch_pseudoinstruction
1000      @pair_elim #len' #assembledi' #eq_assembly_1_pseudoinstruction #IH
1001      cases (IH ?) -IH
1002      [2: %1 cases OR_lt_eq
1003        [ normalize nodelta #LT lapply LT >nat_of_bitvector_add
1004          [2: >nat_of_bitvector_bitvector_of_nat_inverse [2: //]
1005            cases (le_to_or_lt_eq … (? : nat_of_bitvector … ppc < 2^16))
1006            [ #X <plus_n_Sm <plus_n_O @X
1007            | #abs @⊥
1008              <(bitvector_of_nat_inverse_nat_of_bitvector … ppc) in LT;
1009              >add_overflow [2: <plus_n_Sm <plus_n_O assumption ]
1010              #abs' @(absurd … abs') normalize in ⊢ (? (??%));
1011              @not_le_Sn_O
1012            | @(lt_to_le_to_lt … ppc_ok) assumption ]]
1013          >nat_of_bitvector_bitvector_of_nat_inverse [2: // ]
1014          <plus_n_Sm <plus_n_O #X lapply (le_S_S_to_le … X) -X #X
1015          cases (le_to_or_lt_eq … X) [//] #abs @⊥
1016          lapply (eq_f … (bitvector_of_nat 16) … abs)
1017          >bitvector_of_nat_inverse_nat_of_bitvector
1018          >bitvector_of_nat_inverse_nat_of_bitvector #EQ
1019          @(absurd … EQ NEQppc')
1020        | >length_append <plus_n_Sm <plus_n_O #EQ @le_S_S_to_le >IH1
1021          >nat_of_bitvector_bitvector_of_nat_inverse try assumption
1022          cases (le_to_or_lt_eq … (lt_nat_of_bitvector 16 ppc')) [#X >EQ @X]
1023          #abs @⊥ <EQ in abs; #X lapply (injective_S … X) #abs
1024          lapply (eq_f … (bitvector_of_nat 16) … abs)
1025          >bitvector_of_nat_inverse_nat_of_bitvector <IH1 #EQ
1026          @(absurd … EQ NEQppc') ]]
1027      #IH6 #IH
1028      change with (let 〈len,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi' in ? ∧ ∀j:ℕ. ∀H:j<|assembledi|.?)
1029      >eq_assembly_1_pseudoinstruction %
1030      [ >reverse_append >length_append
1031        >(fst_snd_assembly_1_pseudoinstruction … eq_assembly_1_pseudoinstruction)
1032        @(transitive_le … IH6) //
1033      | #j #LTj >reverse_append >reverse_reverse cases (IH … LTj) -IH #K #IH %
1034        [ >length_append @(lt_to_le_to_lt … K) //
1035        | >IH @shift_nth_prefix ]]]]]
1036qed.
1037
1038(*
1039definition assembly_unlabelled_program:
1040    assembly_program → option labelled_object_code ≝
1041  λp.
1042    Some …
1043     (mk_labelled_object_code
1044     (foldr … (λi,l. assembly1 i @ l) [ ] p) 〈Stub …, Stub …〉〉).
1045*)
1046
1047definition ticks_of_instruction ≝
1048  λi.
1049    let trivial_code_memory ≝ assembly1 i in
1050    let trivial_status ≝ load_code_memory trivial_code_memory in
1051      \snd (fetch trivial_status (zero ?)).
1052
1053definition ticks_of0:
1054    ∀p:pseudo_assembly_program.
1055      (Identifier → Word) → (Word → Word) → (Word → bool) → Word → pseudo_instruction → nat × nat ≝
1056  λprogram: pseudo_assembly_program.
1057  λlookup_labels: Identifier → Word.
1058  λsigma.
1059  λpolicy.
1060  λppc: Word.
1061  λfetched.
1062    match fetched with
1063    [ Instruction instr ⇒
1064      match instr with
1065      [ JC lbl ⇒
1066        let lookup_address ≝ sigma (lookup_labels lbl) in
1067        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1068        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1069          if sj_possible then
1070            〈2, 2〉
1071          else
1072            〈4, 4〉
1073      | JMP _ ⇒ ? (*CSC: To be implemented*)
1074      | JNC lbl ⇒
1075        let lookup_address ≝ sigma (lookup_labels lbl) in
1076        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1077        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1078          if sj_possible then
1079            〈2, 2〉
1080          else
1081            〈4, 4〉
1082      | JB bit lbl ⇒
1083        let lookup_address ≝ sigma (lookup_labels lbl) in
1084        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1085        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1086          if sj_possible then
1087            〈2, 2〉
1088          else
1089            〈4, 4〉
1090      | JNB bit lbl ⇒
1091        let lookup_address ≝ sigma (lookup_labels lbl) in
1092        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1093        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1094          if sj_possible then
1095            〈2, 2〉
1096          else
1097            〈4, 4〉
1098      | JBC bit lbl ⇒
1099        let lookup_address ≝ sigma (lookup_labels lbl) in
1100        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1101        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1102          if sj_possible then
1103            〈2, 2〉
1104          else
1105            〈4, 4〉
1106      | JZ lbl ⇒
1107        let lookup_address ≝ sigma (lookup_labels lbl) in
1108        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1109        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1110          if sj_possible then
1111            〈2, 2〉
1112          else
1113            〈4, 4〉
1114      | JNZ lbl ⇒
1115        let lookup_address ≝ sigma (lookup_labels lbl) in
1116        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1117        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1118          if sj_possible then
1119            〈2, 2〉
1120          else
1121            〈4, 4〉
1122      | CJNE arg lbl ⇒
1123        let lookup_address ≝ sigma (lookup_labels lbl) in
1124        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1125        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1126          if sj_possible then
1127            〈2, 2〉
1128          else
1129            〈4, 4〉
1130      | DJNZ arg lbl ⇒
1131        let lookup_address ≝ sigma (lookup_labels lbl) in
1132        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1133        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1134          if sj_possible then
1135            〈2, 2〉
1136          else
1137            〈4, 4〉
1138      | ADD arg1 arg2 ⇒
1139        let ticks ≝ ticks_of_instruction (ADD ? arg1 arg2) in
1140         〈ticks, ticks〉
1141      | ADDC arg1 arg2 ⇒
1142        let ticks ≝ ticks_of_instruction (ADDC ? arg1 arg2) in
1143         〈ticks, ticks〉
1144      | SUBB arg1 arg2 ⇒
1145        let ticks ≝ ticks_of_instruction (SUBB ? arg1 arg2) in
1146         〈ticks, ticks〉
1147      | INC arg ⇒
1148        let ticks ≝ ticks_of_instruction (INC ? arg) in
1149         〈ticks, ticks〉
1150      | DEC arg ⇒
1151        let ticks ≝ ticks_of_instruction (DEC ? arg) in
1152         〈ticks, ticks〉
1153      | MUL arg1 arg2 ⇒
1154        let ticks ≝ ticks_of_instruction (MUL ? arg1 arg2) in
1155         〈ticks, ticks〉
1156      | DIV arg1 arg2 ⇒
1157        let ticks ≝ ticks_of_instruction (DIV ? arg1 arg2) in
1158         〈ticks, ticks〉
1159      | DA arg ⇒
1160        let ticks ≝ ticks_of_instruction (DA ? arg) in
1161         〈ticks, ticks〉
1162      | ANL arg ⇒
1163        let ticks ≝ ticks_of_instruction (ANL ? arg) in
1164         〈ticks, ticks〉
1165      | ORL arg ⇒
1166        let ticks ≝ ticks_of_instruction (ORL ? arg) in
1167         〈ticks, ticks〉
1168      | XRL arg ⇒
1169        let ticks ≝ ticks_of_instruction (XRL ? arg) in
1170         〈ticks, ticks〉
1171      | CLR arg ⇒
1172        let ticks ≝ ticks_of_instruction (CLR ? arg) in
1173         〈ticks, ticks〉
1174      | CPL arg ⇒
1175        let ticks ≝ ticks_of_instruction (CPL ? arg) in
1176         〈ticks, ticks〉
1177      | RL arg ⇒
1178        let ticks ≝ ticks_of_instruction (RL ? arg) in
1179         〈ticks, ticks〉
1180      | RLC arg ⇒
1181        let ticks ≝ ticks_of_instruction (RLC ? arg) in
1182         〈ticks, ticks〉
1183      | RR arg ⇒
1184        let ticks ≝ ticks_of_instruction (RR ? arg) in
1185         〈ticks, ticks〉
1186      | RRC arg ⇒
1187        let ticks ≝ ticks_of_instruction (RRC ? arg) in
1188         〈ticks, ticks〉
1189      | SWAP arg ⇒
1190        let ticks ≝ ticks_of_instruction (SWAP ? arg) in
1191         〈ticks, ticks〉
1192      | MOV arg ⇒
1193        let ticks ≝ ticks_of_instruction (MOV ? arg) in
1194         〈ticks, ticks〉
1195      | MOVX arg ⇒
1196        let ticks ≝ ticks_of_instruction (MOVX ? arg) in
1197         〈ticks, ticks〉
1198      | SETB arg ⇒
1199        let ticks ≝ ticks_of_instruction (SETB ? arg) in
1200         〈ticks, ticks〉
1201      | PUSH arg ⇒
1202        let ticks ≝ ticks_of_instruction (PUSH ? arg) in
1203         〈ticks, ticks〉
1204      | POP arg ⇒
1205        let ticks ≝ ticks_of_instruction (POP ? arg) in
1206         〈ticks, ticks〉
1207      | XCH arg1 arg2 ⇒
1208        let ticks ≝ ticks_of_instruction (XCH ? arg1 arg2) in
1209         〈ticks, ticks〉
1210      | XCHD arg1 arg2 ⇒
1211        let ticks ≝ ticks_of_instruction (XCHD ? arg1 arg2) in
1212         〈ticks, ticks〉
1213      | RET ⇒
1214        let ticks ≝ ticks_of_instruction (RET ?) in
1215         〈ticks, ticks〉
1216      | RETI ⇒
1217        let ticks ≝ ticks_of_instruction (RETI ?) in
1218         〈ticks, ticks〉
1219      | NOP ⇒
1220        let ticks ≝ ticks_of_instruction (NOP ?) in
1221         〈ticks, ticks〉
1222      ]
1223    | Comment comment ⇒ 〈0, 0〉
1224    | Cost cost ⇒ 〈0, 0〉
1225    | Jnz _ _ _ ⇒ ? (*CSC: To be implemented*)
1226    | MovSuccessor _ _ _ ⇒ ? (*CSC: To be implemented *)
1227    | Jmp jmp ⇒
1228      let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1229      let do_a_long ≝ policy ppc in
1230      let lookup_address ≝ sigma (lookup_labels jmp) in
1231      let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1232        if sj_possible ∧ ¬ do_a_long then
1233          〈2, 2〉 (* XXX: SJMP *)
1234        else
1235        let 〈mj_possible, disp2〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
1236          if mj_possible ∧ ¬ do_a_long then
1237            〈2, 2〉 (* XXX: AJMP *)
1238          else
1239            〈2, 2〉 (* XXX: LJMP *)
1240    | Call call ⇒
1241      (* XXX: collapse the branches as they are identical? *)
1242      let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1243      let lookup_address ≝ sigma (lookup_labels call) in
1244      let 〈mj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
1245      let do_a_long ≝ policy ppc in
1246      if mj_possible ∧ ¬ do_a_long then
1247        〈2, 2〉 (* ACALL *)
1248      else
1249        〈2, 2〉 (* LCALL *)
1250    | Mov dptr tgt ⇒ 〈2, 2〉
1251    ].
1252cases daemon (*CSC: The three new pseudoinstruction cases*)
1253qed.
1254
1255definition ticks_of:
1256    ∀p:pseudo_assembly_program.
1257    ∀sigma:Word → Word. ∀policy:Word → bool.
1258     ∀ppc:Word.
1259      nat_of_bitvector … ppc < |code p| → nat × nat ≝
1260  λprogram: pseudo_assembly_program.
1261  λsigma.
1262  λpolicy.
1263  λppc: Word. λppc_ok.
1264    let 〈labels, costs〉 ≝ create_label_cost_map (code program) in
1265    let addr_of ≝ λid.bitvector_of_nat 16 (lookup_def ASMTag ℕ labels id O) in
1266    let 〈fetched, new_ppc〉 ≝ fetch_pseudo_instruction (code program) ppc ppc_ok in
1267     ticks_of0 program addr_of sigma policy ppc fetched.
Note: See TracBrowser for help on using the repository browser.