source: src/ASM/Assembly.ma @ 3051

Last change on this file since 3051 was 3039, checked in by tranquil, 7 years ago
  • merged and extended MovSuccessor? and Mov in one instruction (Mov dst

ident offset)

  • JMP now correctly uses ACCDPTR argument
  • LINToASM: ADDRESS now translate to a symbolical Mov (now a preamble

is generated), and globals initialization is fixed accordingly

File size: 59.6 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 ⇒ [ NOP … ]
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 off ⇒
541    let addr ≝ \fst (add_16_with_carry … (lookup_datalabels trgt) off false) in
542    match d with
543    [ inl _ ⇒
544      let address ≝ DATA16 addr in
545      [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))]
546    | inr pr ⇒
547      let v ≝ DATA (match \snd pr with
548        [ LOW ⇒ \snd (vsplit … 8 8 addr)
549        | HIGH ⇒ \fst (vsplit … 8 8 addr)
550        ]) in
551     match \fst pr return λx. bool_to_Prop (is_in ? [[acc_a;direct;registr]] x) → ? with
552     [ ACC_A ⇒ λ_.
553        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? 〈ACC_A, v〉))))))]     
554     | DIRECT b1 ⇒ λ_.
555        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inr ? ? 〈DIRECT b1, v〉)))))]
556     | REGISTER r ⇒ λ_.
557        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? (inr ? ? 〈REGISTER r, v〉))))))]
558     | _ ⇒ Ⓧ ] (subaddressing_modein …)]
559  | Instruction instr ⇒ expand_relative_jump lookup_labels sigma policy ppc instr
560  | Jmp jmp ⇒
561    let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
562    let do_a_long ≝ policy ppc in
563    let lookup_address ≝ sigma (lookup_labels jmp) in
564    let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
565    if sj_possible ∧ ¬ do_a_long then
566      let address ≝ RELATIVE disp in
567        [ SJMP address ]
568    else
569      let 〈mj_possible, disp2〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
570      if mj_possible ∧ ¬ do_a_long then
571        let address ≝ ADDR11 disp2 in
572          [ AJMP address ]
573      else   
574        let address ≝ ADDR16 lookup_address in
575        [ LJMP address ]
576  | Jnz acc tgt1 tgt2 ⇒
577     let lookup_address1 ≝ sigma (lookup_labels tgt1) in
578     let lookup_address2 ≝ sigma (lookup_labels tgt2) in
579      (*CSC: we inefficiently use always LJMPs; the policy could
580        choose two SJMPs instead *)
581      [ RealInstruction (JNZ … (RELATIVE (bitvector_of_nat ? 3)));
582        LJMP (ADDR16 lookup_address2);
583        LJMP (ADDR16 lookup_address1) ]
584  (*| MovSuccessor dst ws lbl ⇒
585     let addr ≝ lookup_labels lbl in
586     let 〈high, low〉 ≝ vsplit ? 8 8 addr in
587     let v ≝ DATA match ws with [ HIGH ⇒ high | LOW ⇒ low ] in
588     match dst return λx. bool_to_Prop (is_in ? [[acc_a;direct;registr]] x) → ? with
589     [ ACC_A ⇒ λ_.
590        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? 〈ACC_A, v〉))))))]     
591     | DIRECT b1 ⇒ λ_.
592        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inr ? ? 〈DIRECT b1, v〉)))))]
593     | REGISTER r ⇒ λ_.
594        [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inl ? ? (inl ? ? (inr ? ? 〈REGISTER r, v〉))))))]     | _ ⇒ λK. match K in False with [ ] ] (subaddressing_modein … dst)*)
595  ].
596  try %
597qed.
598 
599definition assembly_1_pseudoinstruction ≝
600  λlookup_labels.
601  λsigma: Word → Word.
602  λpolicy: Word → bool.
603  λppc: Word.
604  λlookup_datalabels.
605  λi.
606  let pseudos ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels i in
607  let mapped ≝ map ? ? assembly1 pseudos in
608  let flattened ≝ flatten ? mapped in
609  let pc_len ≝ length ? flattened in
610   〈pc_len, flattened〉.
611
612definition instruction_size ≝
613  λlookup_labels.
614  λlookup_datalabels.
615  λsigma: Word → Word.
616  λpolicy: Word → bool.
617  λppc.
618  λi.
619    \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels i).
620 
621(* Labels *)
622definition is_label ≝
623  λx:labelled_instruction.λl:Identifier.
624  let 〈lbl,instr〉 ≝ x in
625  match lbl with
626  [ Some l' ⇒ l' = l
627  | _       ⇒ False
628  ].
629
630lemma label_does_not_occur:
631  ∀i:ℕ.∀p:list labelled_instruction.∀l:Identifier.
632  is_label (nth i ? p 〈None ?, Comment EmptyString〉) l → does_not_occur ?? l p = false.
633 #i #p #l generalize in match i; elim p
634 [ #i >nth_nil #H cases H
635 | #h #t #IH #i cases i -i
636   [ cases h #hi #hp cases hi
637     [ normalize #H cases H
638     | #l' #Heq whd in ⊢ (??%?); change with (eq_identifier ? l' l) in match (instruction_matches_identifier ????);
639       whd in Heq; >Heq
640       >eq_identifier_refl / by refl/
641     ]
642   | #i #H whd in match (does_not_occur ????);
643     whd in match (instruction_matches_identifier ????);
644     cases h #hi #hp cases hi normalize nodelta
645     [ @(IH i) @H
646     | #l' @eq_identifier_elim
647       [ normalize / by /
648       | normalize #_ @(IH i) @H
649       ]
650     ]
651   ]
652 ]
653qed.
654
655definition sigma_policy_specification ≝
656  λprogram: pseudo_assembly_program.
657  λsigma: Word → Word.
658  λpolicy: Word → bool.
659  sigma (zero …) = zero … ∧
660  let instr_list ≝ code program in
661  let preamble ≝ preamble program in
662  ∀ppc: Word. ∀ppc_ok: nat_of_bitvector … ppc < |instr_list|.
663    let pc ≝ sigma ppc in
664    let labels ≝ \fst (create_label_cost_map instr_list) in
665    let lookup_labels ≝ λx. bitvector_of_nat 16 (lookup_def … labels x 0) in
666    let lookup_datalabels ≝ λx. lookup_def … (construct_datalabels preamble) x (lookup_labels x) in
667    let instruction ≝ \fst (fetch_pseudo_instruction instr_list ppc ppc_ok) in
668    let next_pc ≝ sigma (add 16 ppc (bitvector_of_nat 16 1)) in
669     next_pc = add 16 pc (bitvector_of_nat … (instruction_size lookup_labels lookup_datalabels sigma policy ppc instruction))
670     ∧
671     (nat_of_bitvector … pc + instruction_size lookup_labels lookup_datalabels sigma policy ppc instruction < 2^16
672     ∨
673     (∀ppc'. ∀ppc_ok':nat_of_bitvector … ppc' < |instr_list|. nat_of_bitvector … ppc < nat_of_bitvector … ppc' →
674       let instruction' ≝ \fst (fetch_pseudo_instruction instr_list ppc' ppc_ok') in
675       instruction_size lookup_labels lookup_datalabels sigma policy ppc' instruction' = 0)
676     ∧
677     nat_of_bitvector … pc + instruction_size lookup_labels lookup_datalabels sigma policy ppc instruction = 2^16).
678
679
680lemma fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels:
681 ∀lookup_labels,sigma,policy,ppc,pi.
682  ∀lookup_datalabels1,lookup_datalabels2.
683   \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels1 pi) =
684   \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels2 pi).
685#lookup_labels #sigma #policy #ppc #pi #lookup_datalabels1 #lookup_datalabels2
686cases pi // * [ #addr #Id #off % ] * #addr * @(subaddressing_mode_elim … addr) //
687qed.
688
689lemma fst_snd_assembly_1_pseudoinstruction:
690 ∀lookup_labels,sigma,policy,ppc,pi,lookup_datalabels,len,assembled.
691   assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi
692   = 〈len,assembled〉 →
693    len = |assembled|.
694#lookup #sigma #policy #ppc #pi #lookup_datalabels #len #assembled
695inversion (assembly_1_pseudoinstruction ??????) #len' #assembled'
696whd in ⊢ (??%? → ?); #EQ1 #EQ2 destruct %
697qed.
698
699(* XXX: easy but tedious *)
700lemma assembly1_lt_128:
701  ∀i: instruction.
702    |(assembly1 i)| < 128.
703  cases daemon
704(* XXX: commented out as takes ages to type check
705  #i cases i
706  try (#assm1 #assm2) try #assm1
707  [8:
708    cases assm1
709    try (#assm1 #assm2) try #assm1
710    whd in match assembly1; normalize nodelta
711    whd in match assembly_preinstruction; normalize nodelta
712    try @(subaddressing_mode_elim … assm2)
713    try @(subaddressing_mode_elim … assm1) try #w try #w' normalize nodelta
714    [32:
715      cases assm1 -assm1 #assm1 normalize nodelta
716      cases assm1 #addr1 #addr2 normalize nodelta
717      [1:
718        @(subaddressing_mode_elim … addr2)
719      |2:
720        @(subaddressing_mode_elim … addr1)
721      ]
722      #w
723    |35,36,37:
724      cases assm1 -assm1 #assm1 normalize nodelta
725      [1,3:
726        cases assm1 -assm1 #assm1 normalize nodelta
727      ]
728      cases assm1 #addr1 #addr2 normalize nodelta
729      @(subaddressing_mode_elim … addr2) try #w
730    |49:
731      cases assm1 -assm1 #assm1 normalize nodelta
732      [1:
733        cases assm1 -assm1 #assm1 normalize nodelta
734        [1:
735          cases assm1 -assm1 #assm1 normalize nodelta
736          [1:
737            cases assm1 -assm1 #assm1 normalize nodelta
738            [1:
739              cases assm1 -assm1 #assm1 normalize nodelta
740            ]
741          ]
742        ]
743      ]
744      cases assm1 #addr1 #addr2 normalize nodelta
745      [1,3,4,5:
746        @(subaddressing_mode_elim … addr2) try #w
747      |*:
748        @(subaddressing_mode_elim … addr1) try #w
749        normalize nodelta
750        [1,2:
751          @(subaddressing_mode_elim … addr2) try #w
752        ]
753      ]
754    |50:
755      cases assm1 -assm1 #assm1 normalize nodelta
756      cases assm1 #addr1 #addr2 normalize nodelta
757      [1:
758        @(subaddressing_mode_elim … addr2) try #w
759      |2:
760        @(subaddressing_mode_elim … addr1) try #w
761      ]
762    ]
763    normalize repeat @le_S_S @le_O_n
764  ]
765  whd in match assembly1; normalize nodelta
766  [6:
767    normalize repeat @le_S_S @le_O_n
768  |7:
769    @(subaddressing_mode_elim … assm2) normalize repeat @le_S_S @le_O_n
770  |*:
771    @(subaddressing_mode_elim … assm1) #w normalize nodelta repeat @le_S_S @le_O_n
772  ]
773  *)
774qed.
775
776lemma assembly1_pseudoinstruction_lt_2_to_16:
777  ∀lookup_labels,sigma,policy,ppc,lookup_datalabels,pi.
778  |\snd (assembly_1_pseudoinstruction
779    lookup_labels sigma policy ppc lookup_datalabels pi)|
780   < 2^16.
781 #lookup_labels #sigma #policy #ppc #lookup_datalabels *
782[ cut (128 < 2^16) [@leb_true_to_le %] #LT
783  * whd in match (assembly_1_pseudoinstruction ??????);
784  whd in match (expand_pseudo_instruction ??????);
785  whd in match assembly_1_pseudoinstruction; normalize nodelta
786  try (#arg1 #arg2 #arg3) try (#arg1 #arg2) try #arg1
787  whd in match (expand_pseudo_instruction ??????);
788  try
789   (change with (|flatten ? [assembly1 ?]| < ?)
790    >flatten_singleton
791    @(transitive_lt … (assembly1_lt_128 ?))
792    @LT)
793  @pair_elim #x #y #_ cases x cases (policy ppc) normalize nodelta
794  try
795   (change with (|flatten ? [assembly1 ?]| < ?)
796    >flatten_singleton
797    @(transitive_lt … (assembly1_lt_128 ?))
798    @LT)
799  change with (|flatten ? [assembly1 ?; assembly1 ?; assembly1 ?]| < ?)
800  >length_flatten_cons >length_flatten_cons >length_flatten_cons <plus_n_O
801  <associative_plus @(transitive_lt … (tech_transitive_lt_3 … (2^7) ???))
802  try @assembly1_lt_128 @leb_true_to_le %
803|2,3: #msg normalize in ⊢ (?%?); //
804| #label whd in match (assembly_1_pseudoinstruction ??????);
805  whd in match (expand_pseudo_instruction ??????);
806  @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_
807  [2: @pair_elim #x #y #_ cases (?∧?)]
808  normalize in ⊢ (?%?); //
809|6: #label whd in match (assembly_1_pseudoinstruction ??????);
810  whd in match (expand_pseudo_instruction ??????);
811  @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_
812  normalize in ⊢ (?%?); //
813|5: #acc #dst1 #dst2 normalize in ⊢ (?%?); //
814|7: * [ #dptr #id #offset normalize in ⊢ (?%?); //]
815    * #dst @(subaddressing_mode_elim … dst) [2,3: #w] * #lbl #off
816    whd in match (assembly_1_pseudoinstruction ??????);
817    whd in match (expand_pseudo_instruction ??????);
818    lapply (vsplit bool 8 8 ?) * #high #low
819    normalize in ⊢ (?%?); //
820]
821qed.
822
823definition assembly:
824    ∀p: pseudo_assembly_program.
825    ∀sigma: Word → Word.
826    ∀policy: Word → bool.
827      Σres:labelled_object_code.
828       sigma_policy_specification p sigma policy →
829       let preamble ≝ preamble p in
830       let instr_list ≝ code p in
831       |instr_list| ≤ 2^16 →
832       let assembled ≝ oc res in
833       |assembled| ≤ 2^16 ∧
834       (nat_of_bitvector … (sigma (bitvector_of_nat … (|instr_list|))) = |assembled| ∨
835        sigma (bitvector_of_nat … (|instr_list|)) = zero … ∧ |assembled| = 2^16) ∧
836       let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in
837       let datalabels ≝ construct_datalabels preamble in
838       let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in
839       let lookup_datalabels ≝ λx.lookup_def … datalabels x (lookup_labels x) in
840       ∀ppc. ∀ppc_ok:nat_of_bitvector … ppc < |instr_list|.
841         let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc ppc_ok in
842         let 〈len,assembledi〉 ≝
843          assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in
844         |assembledi| ≤ |assembled| ∧
845         ∀j:nat. ∀H: j < |assembledi|. ∃K.
846          nth_safe ? j assembledi H =
847           nth_safe ? (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat ? j)))
848            assembled K
849
850  λp.
851  λsigma.
852  λpolicy.
853  deplet 〈labels_to_ppc,ppc_to_costs〉 as eq_create_label_cost_map ≝ create_label_cost_map (code p) in
854  let preamble ≝ preamble p in
855  let instr_list ≝ code p in
856  let datalabels ≝ construct_datalabels preamble in
857  let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in
858  let lookup_datalabels ≝ λx.lookup_def … datalabels x (lookup_labels x) in
859  let 〈next_pc,revcode〉 ≝ pi1 … (
860     foldl_strong
861      (option Identifier × pseudo_instruction)
862      (λpre. Σppc_code:(Word × (list Byte)).
863       sigma_policy_specification p sigma policy →
864        |instr_list| ≤ 2^16 →
865        let 〈ppc,code〉 ≝ ppc_code in
866         ppc = bitvector_of_nat … (|pre|) ∧
867         |code| ≤ 2^16 ∧
868         (nat_of_bitvector … (sigma ppc) = |code| ∨
869          sigma ppc = zero … ∧ |code| = 2^16 ∧
870          (|pre| < 2^16 → ∀ppc'. ∀ppc_ok':nat_of_bitvector … ppc' < |instr_list|. nat_of_bitvector … ppc ≤ nat_of_bitvector … ppc' →
871            let instruction' ≝ \fst (fetch_pseudo_instruction instr_list ppc' ppc_ok') in
872            instruction_size lookup_labels lookup_datalabels sigma policy ppc' instruction' = 0)
873         ) ∧
874         ∀ppc'.∀ppc_ok'.
875          (nat_of_bitvector … ppc' < nat_of_bitvector … ppc ∨ |pre| = 2^16) →
876           let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' ppc_ok' in
877           let 〈len,assembledi〉 ≝
878            assembly_1_pseudoinstruction lookup_labels sigma policy ppc' lookup_datalabels pi in
879           |assembledi| ≤ |reverse … code| ∧
880           ∀j:nat. ∀H: j < |assembledi|. ∃K.
881            nth_safe ? j assembledi H =
882             nth_safe ? (nat_of_bitvector … (add … (sigma ppc') (bitvector_of_nat ? j))) (reverse … code) K)
883      instr_list
884      (λprefix,hd,tl,prf,ppc_code.
885        let 〈ppc, code〉 ≝ pi1 … ppc_code in
886        let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels (\snd hd) in
887        let new_ppc ≝ add ? ppc (bitvector_of_nat ? 1) in
888         〈new_ppc, (reverse … program @ code)〉)
889      〈(zero ?), [ ]〉)
890    in
891     let code ≝ reverse … revcode in
892     mk_labelled_object_code
893     code (load_code_memory code) (refl …)
894     (fold … (λppc.λcost.λpc_to_costs. insert … (sigma ppc) cost pc_to_costs) ppc_to_costs (Stub ??))
895     (foldl ??
896      (λsymboltable,newident_oldident.
897        let ppc ≝ lookup_labels (\fst newident_oldident) in
898         insert … (sigma ppc) (\snd newident_oldident) symboltable) (Stub ??) (renamed_symbols p))
899     (sigma (lookup_labels (final_label p))) ?.
900  [ cases (foldl_strong ? (λx.Σy.?) ???) in p1; #ignore_revcode #Hfold #EQignore_revcode
901    >EQignore_revcode in Hfold; #Hfold #sigma_pol_ok #instr_list_ok
902    cases (Hfold sigma_pol_ok instr_list_ok) -Hfold * * #Hfold1 #Hfold4 #Hfold5 #Hfold3 whd
903    <eq_create_label_cost_map whd %
904    [2: #ppc #LTppc @Hfold3 >Hfold1 @(eqb_elim (|instr_list|) 2^16)
905      [ #limit %2 @limit
906      | #nlimit %1 >nat_of_bitvector_bitvector_of_nat_inverse try assumption
907        @not_eq_to_le_to_lt assumption ]
908    | >length_reverse % try assumption cases Hfold5 -Hfold5
909      [ #Hfold5 <Hfold5 % >Hfold1 %
910      | * #Hfold51 #Hfold52 %2 <Hfold1 assumption ]]
911  | cases daemon
912  | * #sigma_pol_ok1 #_ #instr_list_ok %
913    [ % % [%] // >sigma_pol_ok1 % ]
914    #ppc' #ppc_ok' #abs @⊥ cases abs
915     [#abs2 cases (not_le_Sn_O ?) [#H @(H abs2) | skip]
916     |#abs2 change with (0 = S ?) in abs2; destruct(abs2) ]
917  | #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
918    #IH cases (IH ? instr_list_ok) [2: % assumption ] -IH
919    * * #IH1 #IH2 #IH3 #IH4
920    cut (|prefix| < |instr_list|)
921    [ >prf >length_append normalize <plus_n_Sm @le_S_S // ] #LT_prefix_instr_list
922    cut (|prefix| < 2^16)
923    [ @(lt_to_le_to_lt … (|instr_list|)) assumption ] #prefix_ok
924    cut (nat_of_bitvector … ppc < |instr_list|)
925    [ >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ] #ppc_ok
926    cut (\snd hd = \fst (fetch_pseudo_instruction instr_list ppc ppc_ok))
927    [ >prf in ppc_ok; >IH1 >(add_zero … (bitvector_of_nat … (|prefix|)))
928      >fetch_pseudo_instruction_append
929      [ #ppc_ok whd in match fetch_pseudo_instruction; normalize nodelta
930        whd in match (nth_safe ????); [ cases hd // | normalize // ]
931      | <add_zero >nat_of_bitvector_bitvector_of_nat_inverse
932        [ <prf assumption | assumption ]
933      | skip
934      | <prf assumption
935      ]] #eq_fetch_pseudo_instruction
936    lapply (fst_snd_assembly_1_pseudoinstruction … p2) #EQpc_delta
937    cut (pc_delta < 2^16)
938    [ >EQpc_delta
939      @(eq_ind ?? (λp.λ_. |\snd p| < 2^16) ?? p2)
940      @assembly1_pseudoinstruction_lt_2_to_16 ] #pc_delta_ok
941    cut (pc_delta = instruction_size lookup_labels lookup_datalabels sigma policy ppc (\snd hd))
942    [ whd in match instruction_size; normalize nodelta
943      >fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels [ >p2 | skip] % ]
944    #EQpc_delta2
945    cases (sigma_pol_ok2 … ppc_ok)
946    <eq_fetch_pseudo_instruction <eq_create_label_cost_map <EQpc_delta2
947    #sigma_pol3 #sigma_pol4
948    % [ % [% ] ]
949    [ >length_append normalize nodelta >IH1 @sym_eq @add_bitvector_of_nat
950    | >length_append >length_reverse <EQpc_delta
951      cases IH3 -IH3
952      [ #IH3 <IH3 >commutative_plus
953        cases sigma_pol4 [ #LT @(transitive_le … LT) // | * #_ #EQ >EQ % ]
954      | * * #IH3a #IH3b #IH3c >IH3b <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction
955        >IH3c try % assumption ]
956    | >length_append >length_reverse
957      cases IH3 -IH3
958      [ #IH3 <IH3 <EQpc_delta cases sigma_pol4
959        [ #LT %1 >sigma_pol3 >nat_of_bitvector_add
960          [2: >nat_of_bitvector_bitvector_of_nat_inverse assumption]
961          >nat_of_bitvector_bitvector_of_nat_inverse try assumption //
962        | * #EQ1 #EQ2 %2 %
963          [ lapply (eq_f … (bitvector_of_nat 16) … EQ2) <add_bitvector_of_nat_plus
964            >bitvector_of_nat_inverse_nat_of_bitvector <sigma_pol3 #X >X % //
965          | #LLT_prefix
966            cut (S (nat_of_bitvector … ppc) < 2^16)
967            [ >length_append in LLT_prefix; <plus_n_Sm <plus_n_O #LLT_prefix
968              >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ]
969            -LLT_prefix #LLT_prefix
970            #ppc' #ppc_ok' #LEQ_newppc_ppc' whd >EQ1 try %
971            @(lt_to_le_to_lt … LEQ_newppc_ppc') normalize nodelta
972            >nat_of_bitvector_add >nat_of_bitvector_bitvector_of_nat_inverse // ]]
973      | * * #IH5 #IH6 #IH7 %2 % [% ]
974        [ normalize nodelta >sigma_pol3 >IH5
975          >add_commutative <add_zero >nat_of_bitvector_bitvector_of_nat_inverse try assumption
976          >EQpc_delta2 >eq_fetch_pseudo_instruction >IH7 try % assumption
977        | >IH6 <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction >IH7 try %
978          assumption
979        | #LLT_prefix
980          cut (S (nat_of_bitvector … ppc) < 2^16)
981          [ >length_append in LLT_prefix; <plus_n_Sm <plus_n_O #LLT_prefix
982            >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ]
983          -LLT_prefix #LLT_prefix
984          #ppc' #ppc_ok' #LEQ_newppc_ppc' whd @IH7 try assumption
985          @(transitive_le … LEQ_newppc_ppc') normalize nodelta
986          >nat_of_bitvector_add >nat_of_bitvector_bitvector_of_nat_inverse // ]]
987  | #ppc' #LTppc' cases hd in prf p2 EQpc_delta2 eq_fetch_pseudo_instruction; #label #pi #prf #p2
988    #EQpc_delta2 #eq_fetch_pseudo_instruction #OR_lt_eq @(eq_bv_elim … ppc' ppc)
989    [ #EQppc' >EQppc' in LTppc'; -ppc' >prf
990      >IH1 #LTppc lapply LTppc
991      >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → match % with [_ ⇒ ?]);
992      >fetch_pseudo_instruction_append
993      [3: @le_S_S @le_O_n
994      |2: lapply LTppc; >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → ?); #H @H
995      |4: <prf @instr_list_ok ]
996      #LTppc' @pair_elim #pi' #newppc' #EQpair destruct(EQpair) <IH1 >p2 %
997      [ >length_reverse >length_append >length_reverse // ]
998      #j #LTj >nat_of_bitvector_add
999      >nat_of_bitvector_bitvector_of_nat_inverse
1000      [2,4: @(lt_to_le_to_lt … LTj) <EQpc_delta @(transitive_le … pc_delta_ok) %2 %
1001      |3: @(lt_to_le_to_lt … (nat_of_bitvector … (sigma ppc) + pc_delta))
1002          [ >EQpc_delta @monotonic_lt_plus_r assumption
1003          | cases sigma_pol4
1004            [ #H @(transitive_le … H) %2 %
1005            | * #_ #EQ >EQ % ]]]
1006      >reverse_append >reverse_reverse
1007      cases IH3 -IH3
1008      [ #IH3 >IH3 <(length_reverse … code) %
1009        [ >length_append @monotonic_lt_plus_r assumption
1010        | @nth_safe_prepend ]
1011      | * * #IH3a #IH3b #IH3c >IH3a @⊥
1012        cut (|program| = 0)
1013        [ <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction @IH3c // ] #EQprogram
1014        @(absurd ?? (not_le_Sn_O j)) <EQprogram assumption ]
1015    | #NEQppc'
1016      lapply (IH4 … LTppc')
1017      @pair_elim #pi' #newppc' #eq_fetch_pseudoinstruction
1018      @pair_elim #len' #assembledi' #eq_assembly_1_pseudoinstruction #IH
1019      cases (IH ?) -IH
1020      [2: %1 cases OR_lt_eq
1021        [ normalize nodelta #LT lapply LT >nat_of_bitvector_add
1022          [2: >nat_of_bitvector_bitvector_of_nat_inverse [2: //]
1023            cases (le_to_or_lt_eq … (? : nat_of_bitvector … ppc < 2^16))
1024            [ #X <plus_n_Sm <plus_n_O @X
1025            | #abs @⊥
1026              <(bitvector_of_nat_inverse_nat_of_bitvector … ppc) in LT;
1027              >add_overflow [2: <plus_n_Sm <plus_n_O assumption ]
1028              #abs' @(absurd … abs') normalize in ⊢ (? (??%));
1029              @not_le_Sn_O
1030            | @(lt_to_le_to_lt … ppc_ok) assumption ]]
1031          >nat_of_bitvector_bitvector_of_nat_inverse [2: // ]
1032          <plus_n_Sm <plus_n_O #X lapply (le_S_S_to_le … X) -X #X
1033          cases (le_to_or_lt_eq … X) [//] #abs @⊥
1034          lapply (eq_f … (bitvector_of_nat 16) … abs)
1035          >bitvector_of_nat_inverse_nat_of_bitvector
1036          >bitvector_of_nat_inverse_nat_of_bitvector #EQ
1037          @(absurd … EQ NEQppc')
1038        | >length_append <plus_n_Sm <plus_n_O #EQ @le_S_S_to_le >IH1
1039          >nat_of_bitvector_bitvector_of_nat_inverse try assumption
1040          cases (le_to_or_lt_eq … (lt_nat_of_bitvector 16 ppc')) [#X >EQ @X]
1041          #abs @⊥ <EQ in abs; #X lapply (injective_S … X) #abs
1042          lapply (eq_f … (bitvector_of_nat 16) … abs)
1043          >bitvector_of_nat_inverse_nat_of_bitvector <IH1 #EQ
1044          @(absurd … EQ NEQppc') ]]
1045      #IH6 #IH
1046      change with (let 〈len,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi' in ? ∧ ∀j:ℕ. ∀H:j<|assembledi|.?)
1047      >eq_assembly_1_pseudoinstruction %
1048      [ >reverse_append >length_append
1049        >(fst_snd_assembly_1_pseudoinstruction … eq_assembly_1_pseudoinstruction)
1050        @(transitive_le … IH6) //
1051      | #j #LTj >reverse_append >reverse_reverse cases (IH … LTj) -IH #K #IH %
1052        [ >length_append @(lt_to_le_to_lt … K) //
1053        | >IH @shift_nth_prefix ]]]]]
1054qed.
1055
1056(*
1057definition assembly_unlabelled_program:
1058    assembly_program → option labelled_object_code ≝
1059  λp.
1060    Some …
1061     (mk_labelled_object_code
1062     (foldr … (λi,l. assembly1 i @ l) [ ] p) 〈Stub …, Stub …〉〉).
1063*)
1064
1065definition ticks_of_instruction ≝
1066  λi.
1067    let trivial_code_memory ≝ assembly1 i in
1068    let trivial_status ≝ load_code_memory trivial_code_memory in
1069      \snd (fetch trivial_status (zero ?)).
1070
1071definition ticks_of0:
1072    ∀p:pseudo_assembly_program.
1073      (Identifier → Word) → (Word → Word) → (Word → bool) → Word → pseudo_instruction → nat × nat ≝
1074  λprogram: pseudo_assembly_program.
1075  λlookup_labels: Identifier → Word.
1076  λsigma.
1077  λpolicy.
1078  λppc: Word.
1079  λfetched.
1080    match fetched with
1081    [ Instruction instr ⇒
1082      match instr with
1083      [ JC lbl ⇒
1084        let lookup_address ≝ sigma (lookup_labels lbl) in
1085        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1086        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1087          if sj_possible then
1088            〈2, 2〉
1089          else
1090            〈4, 4〉
1091      | JMP _ ⇒ 〈2, 2〉
1092      | JNC lbl ⇒
1093        let lookup_address ≝ sigma (lookup_labels lbl) in
1094        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1095        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1096          if sj_possible then
1097            〈2, 2〉
1098          else
1099            〈4, 4〉
1100      | JB bit lbl ⇒
1101        let lookup_address ≝ sigma (lookup_labels lbl) in
1102        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1103        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1104          if sj_possible then
1105            〈2, 2〉
1106          else
1107            〈4, 4〉
1108      | JNB bit lbl ⇒
1109        let lookup_address ≝ sigma (lookup_labels lbl) in
1110        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1111        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1112          if sj_possible then
1113            〈2, 2〉
1114          else
1115            〈4, 4〉
1116      | JBC bit lbl ⇒
1117        let lookup_address ≝ sigma (lookup_labels lbl) in
1118        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1119        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1120          if sj_possible then
1121            〈2, 2〉
1122          else
1123            〈4, 4〉
1124      | JZ lbl ⇒
1125        let lookup_address ≝ sigma (lookup_labels lbl) in
1126        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1127        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1128          if sj_possible then
1129            〈2, 2〉
1130          else
1131            〈4, 4〉
1132      | JNZ lbl ⇒
1133        let lookup_address ≝ sigma (lookup_labels lbl) in
1134        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1135        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1136          if sj_possible then
1137            〈2, 2〉
1138          else
1139            〈4, 4〉
1140      | CJNE arg lbl ⇒
1141        let lookup_address ≝ sigma (lookup_labels lbl) in
1142        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1143        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1144          if sj_possible then
1145            〈2, 2〉
1146          else
1147            〈4, 4〉
1148      | DJNZ arg lbl ⇒
1149        let lookup_address ≝ sigma (lookup_labels lbl) in
1150        let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1151        let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1152          if sj_possible then
1153            〈2, 2〉
1154          else
1155            〈4, 4〉
1156      | ADD arg1 arg2 ⇒
1157        let ticks ≝ ticks_of_instruction (ADD ? arg1 arg2) in
1158         〈ticks, ticks〉
1159      | ADDC arg1 arg2 ⇒
1160        let ticks ≝ ticks_of_instruction (ADDC ? arg1 arg2) in
1161         〈ticks, ticks〉
1162      | SUBB arg1 arg2 ⇒
1163        let ticks ≝ ticks_of_instruction (SUBB ? arg1 arg2) in
1164         〈ticks, ticks〉
1165      | INC arg ⇒
1166        let ticks ≝ ticks_of_instruction (INC ? arg) in
1167         〈ticks, ticks〉
1168      | DEC arg ⇒
1169        let ticks ≝ ticks_of_instruction (DEC ? arg) in
1170         〈ticks, ticks〉
1171      | MUL arg1 arg2 ⇒
1172        let ticks ≝ ticks_of_instruction (MUL ? arg1 arg2) in
1173         〈ticks, ticks〉
1174      | DIV arg1 arg2 ⇒
1175        let ticks ≝ ticks_of_instruction (DIV ? arg1 arg2) in
1176         〈ticks, ticks〉
1177      | DA arg ⇒
1178        let ticks ≝ ticks_of_instruction (DA ? arg) in
1179         〈ticks, ticks〉
1180      | ANL arg ⇒
1181        let ticks ≝ ticks_of_instruction (ANL ? arg) in
1182         〈ticks, ticks〉
1183      | ORL arg ⇒
1184        let ticks ≝ ticks_of_instruction (ORL ? arg) in
1185         〈ticks, ticks〉
1186      | XRL arg ⇒
1187        let ticks ≝ ticks_of_instruction (XRL ? arg) in
1188         〈ticks, ticks〉
1189      | CLR arg ⇒
1190        let ticks ≝ ticks_of_instruction (CLR ? arg) in
1191         〈ticks, ticks〉
1192      | CPL arg ⇒
1193        let ticks ≝ ticks_of_instruction (CPL ? arg) in
1194         〈ticks, ticks〉
1195      | RL arg ⇒
1196        let ticks ≝ ticks_of_instruction (RL ? arg) in
1197         〈ticks, ticks〉
1198      | RLC arg ⇒
1199        let ticks ≝ ticks_of_instruction (RLC ? arg) in
1200         〈ticks, ticks〉
1201      | RR arg ⇒
1202        let ticks ≝ ticks_of_instruction (RR ? arg) in
1203         〈ticks, ticks〉
1204      | RRC arg ⇒
1205        let ticks ≝ ticks_of_instruction (RRC ? arg) in
1206         〈ticks, ticks〉
1207      | SWAP arg ⇒
1208        let ticks ≝ ticks_of_instruction (SWAP ? arg) in
1209         〈ticks, ticks〉
1210      | MOV arg ⇒
1211        let ticks ≝ ticks_of_instruction (MOV ? arg) in
1212         〈ticks, ticks〉
1213      | MOVX arg ⇒
1214        let ticks ≝ ticks_of_instruction (MOVX ? arg) in
1215         〈ticks, ticks〉
1216      | SETB arg ⇒
1217        let ticks ≝ ticks_of_instruction (SETB ? arg) in
1218         〈ticks, ticks〉
1219      | PUSH arg ⇒
1220        let ticks ≝ ticks_of_instruction (PUSH ? arg) in
1221         〈ticks, ticks〉
1222      | POP arg ⇒
1223        let ticks ≝ ticks_of_instruction (POP ? arg) in
1224         〈ticks, ticks〉
1225      | XCH arg1 arg2 ⇒
1226        let ticks ≝ ticks_of_instruction (XCH ? arg1 arg2) in
1227         〈ticks, ticks〉
1228      | XCHD arg1 arg2 ⇒
1229        let ticks ≝ ticks_of_instruction (XCHD ? arg1 arg2) in
1230         〈ticks, ticks〉
1231      | RET ⇒
1232        let ticks ≝ ticks_of_instruction (RET ?) in
1233         〈ticks, ticks〉
1234      | RETI ⇒
1235        let ticks ≝ ticks_of_instruction (RETI ?) in
1236         〈ticks, ticks〉
1237      | NOP ⇒
1238        let ticks ≝ ticks_of_instruction (NOP ?) in
1239         〈ticks, ticks〉
1240      ]
1241    | Comment comment ⇒ 〈0, 0〉
1242    | Cost cost ⇒
1243       let ticks ≝ ticks_of_instruction (NOP ?) in
1244         〈ticks, ticks〉
1245    | Jnz _ _ _ ⇒ 〈4, 4〉
1246    | Jmp jmp ⇒
1247      let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1248      let do_a_long ≝ policy ppc in
1249      let lookup_address ≝ sigma (lookup_labels jmp) in
1250      let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in
1251        if sj_possible ∧ ¬ do_a_long then
1252          〈2, 2〉 (* XXX: SJMP *)
1253        else
1254        let 〈mj_possible, disp2〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
1255          if mj_possible ∧ ¬ do_a_long then
1256            〈2, 2〉 (* XXX: AJMP *)
1257          else
1258            〈2, 2〉 (* XXX: LJMP *)
1259    | Call call ⇒
1260      (* XXX: collapse the branches as they are identical? *)
1261      let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in
1262      let lookup_address ≝ sigma (lookup_labels call) in
1263      let 〈mj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in
1264      let do_a_long ≝ policy ppc in
1265      if mj_possible ∧ ¬ do_a_long then
1266        〈2, 2〉 (* ACALL *)
1267      else
1268        〈2, 2〉 (* LCALL *)
1269    | Mov dst lbl off ⇒
1270      match dst with
1271      [ inl _ ⇒ 〈2, 2〉
1272      | inr pr ⇒
1273        match \fst pr return λx.is_in … [[ acc_a; direct; registr]] x → ? with
1274        [ REGISTER r ⇒ λ_.〈1, 1〉
1275        | DIRECT d ⇒ λ_.〈2, 2〉
1276        | ACC_A ⇒ λ_.〈1, 1〉
1277        | _ ⇒ Ⓧ] (subaddressing_modein …)]
1278     ].
1279
1280definition ticks_of:
1281    ∀p:pseudo_assembly_program.
1282    ∀sigma:Word → Word. ∀policy:Word → bool.
1283     ∀ppc:Word.
1284      nat_of_bitvector … ppc < |code p| → nat × nat ≝
1285  λprogram: pseudo_assembly_program.
1286  λsigma.
1287  λpolicy.
1288  λppc: Word. λppc_ok.
1289    let 〈labels, costs〉 ≝ create_label_cost_map (code program) in
1290    let addr_of ≝ λid.bitvector_of_nat 16 (lookup_def ASMTag ℕ labels id O) in
1291    let 〈fetched, new_ppc〉 ≝ fetch_pseudo_instruction (code program) ppc ppc_ok in
1292     ticks_of0 program addr_of sigma policy ppc fetched.
Note: See TracBrowser for help on using the repository browser.