1 | include "ASM/ASM.ma". |
---|
2 | include "ASM/BitVectorTrie.ma". |
---|
3 | include "ASM/Arithmetic.ma". |
---|
4 | include "ASM/Fetch.ma". |
---|
5 | include "ASM/Status.ma". |
---|
6 | |
---|
7 | definition assembly_preinstruction ≝ |
---|
8 | λA: Type[0]. |
---|
9 | λaddr_of: A → Byte. (* relative *) |
---|
10 | λpre: preinstruction A. |
---|
11 | match pre with |
---|
12 | [ ADD addr1 addr2 ⇒ |
---|
13 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
14 | [ REGISTER r ⇒ λ_.[ ([[false;false;true;false;true]]) @@ r ] |
---|
15 | | DIRECT b1 ⇒ λ_.[ ([[false;false;true;false;false;true;false;true]]); b1 ] |
---|
16 | | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;false;false;true;true;i1]]) ] |
---|
17 | | DATA b1 ⇒ λ_. [ ([[false;false;true;false;false;true;false;false]]) ; b1 ] |
---|
18 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
19 | | ADDC addr1 addr2 ⇒ |
---|
20 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
21 | [ REGISTER r ⇒ λ_.[ ([[false;false;true;true;true]]) @@ r ] |
---|
22 | | DIRECT b1 ⇒ λ_.[ ([[false;false;true;true;false;true;false;true]]); b1 ] |
---|
23 | | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;true;false;true;true;i1]]) ] |
---|
24 | | DATA b1 ⇒ λ_. [ ([[false;false;true;true;false;true;false;false]]) ; b1 ] |
---|
25 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
26 | | ANL addrs ⇒ |
---|
27 | match addrs with |
---|
28 | [ inl addrs ⇒ match addrs with |
---|
29 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
30 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
31 | [ REGISTER r ⇒ λ_.[ ([[false;true;false;true;true]]) @@ r ] |
---|
32 | | DIRECT b1 ⇒ λ_.[ ([[false;true;false;true;false;true;false;true]]); b1 ] |
---|
33 | | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;true;false;true;true;i1]]) ] |
---|
34 | | DATA b1 ⇒ λ_. [ ([[false;true;false;true;false;true;false;false]]) ; b1 ] |
---|
35 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
36 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
37 | let b1 ≝ |
---|
38 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
39 | [ DIRECT b1 ⇒ λ_.b1 |
---|
40 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
41 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
42 | [ ACC_A ⇒ λ_.[ ([[false;true;false;true;false;false;true;false]]) ; b1 ] |
---|
43 | | DATA b2 ⇒ λ_. [ ([[false;true;false;true;false;false;true;true]]) ; b1 ; b2 ] |
---|
44 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
45 | ] |
---|
46 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
47 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with |
---|
48 | [ BIT_ADDR b1 ⇒ λ_.[ ([[true;false;false;false;false;false;true;false]]) ; b1 ] |
---|
49 | | N_BIT_ADDR b1 ⇒ λ_. [ ([[true;false;true;true;false;false;false;false]]) ; b1 ] |
---|
50 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
51 | | CLR addr ⇒ |
---|
52 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with |
---|
53 | [ ACC_A ⇒ λ_. |
---|
54 | [ ([[true; true; true; false; false; true; false; false]]) ] |
---|
55 | | CARRY ⇒ λ_. |
---|
56 | [ ([[true; true; false; false; false; false; true; true]]) ] |
---|
57 | | BIT_ADDR b1 ⇒ λ_. |
---|
58 | [ ([[true; true; false; false; false; false; true; false]]) ; b1 ] |
---|
59 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
60 | | CPL addr ⇒ |
---|
61 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with |
---|
62 | [ ACC_A ⇒ λ_. |
---|
63 | [ ([[true; true; true; true; false; true; false; false]]) ] |
---|
64 | | CARRY ⇒ λ_. |
---|
65 | [ ([[true; false; true; true; false; false; true; true]]) ] |
---|
66 | | BIT_ADDR b1 ⇒ λ_. |
---|
67 | [ ([[true; false; true; true; false; false; true; false]]) ; b1 ] |
---|
68 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
69 | | DA addr ⇒ |
---|
70 | [ ([[true; true; false; true; false; true; false; false]]) ] |
---|
71 | | DEC addr ⇒ |
---|
72 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with |
---|
73 | [ ACC_A ⇒ λ_. |
---|
74 | [ ([[false; false; false; true; false; true; false; false]]) ] |
---|
75 | | REGISTER r ⇒ λ_. |
---|
76 | [ ([[false; false; false; true; true]]) @@ r ] |
---|
77 | | DIRECT b1 ⇒ λ_. |
---|
78 | [ ([[false; false; false; true; false; true; false; true]]); b1 ] |
---|
79 | | INDIRECT i1 ⇒ λ_. |
---|
80 | [ ([[false; false; false; true; false; true; true; i1]]) ] |
---|
81 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
82 | | DJNZ addr1 addr2 ⇒ |
---|
83 | let b2 ≝ addr_of addr2 in |
---|
84 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with |
---|
85 | [ REGISTER r ⇒ λ_. |
---|
86 | [ ([[true; true; false; true; true]]) @@ r ; b2 ] |
---|
87 | | DIRECT b1 ⇒ λ_. |
---|
88 | [ ([[true; true; false; true; false; true; false; true]]); b1; b2 ] |
---|
89 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
90 | | JC addr ⇒ |
---|
91 | let b1 ≝ addr_of addr in |
---|
92 | [ ([[false; true; false; false; false; false; false; false]]); b1 ] |
---|
93 | | JNC addr ⇒ |
---|
94 | let b1 ≝ addr_of addr in |
---|
95 | [ ([[false; true; false; true; false; false; false; false]]); b1 ] |
---|
96 | | JZ addr ⇒ |
---|
97 | let b1 ≝ addr_of addr in |
---|
98 | [ ([[false; true; true; false; false; false; false; false]]); b1 ] |
---|
99 | | JNZ addr ⇒ |
---|
100 | let b1 ≝ addr_of addr in |
---|
101 | [ ([[false; true; true; true; false; false; false; false]]); b1 ] |
---|
102 | | JB addr1 addr2 ⇒ |
---|
103 | let b2 ≝ addr_of addr2 in |
---|
104 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
105 | [ BIT_ADDR b1 ⇒ λ_. |
---|
106 | [ ([[false; false; true; false; false; false; false; false]]); b1; b2 ] |
---|
107 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
108 | | JNB addr1 addr2 ⇒ |
---|
109 | let b2 ≝ addr_of addr2 in |
---|
110 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
111 | [ BIT_ADDR b1 ⇒ λ_. |
---|
112 | [ ([[false; false; true; true; false; false; false; false]]); b1; b2 ] |
---|
113 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
114 | | JBC addr1 addr2 ⇒ |
---|
115 | let b2 ≝ addr_of addr2 in |
---|
116 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
117 | [ BIT_ADDR b1 ⇒ λ_. |
---|
118 | [ ([[false; false; false; true; false; false; false; false]]); b1; b2 ] |
---|
119 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
120 | | CJNE addrs addr3 ⇒ |
---|
121 | let b3 ≝ addr_of addr3 in |
---|
122 | match addrs with |
---|
123 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
124 | match addr2 return λx. bool_to_Prop (is_in ? [[direct;data]] x) → ? with |
---|
125 | [ DIRECT b1 ⇒ λ_. |
---|
126 | [ ([[true; false; true; true; false; true; false; true]]); b1; b3 ] |
---|
127 | | DATA b1 ⇒ λ_. |
---|
128 | [ ([[true; false; true; true; false; true; false; false]]); b1; b3 ] |
---|
129 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
130 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
131 | let b2 ≝ |
---|
132 | match addr2 return λx. bool_to_Prop (is_in ? [[data]] x) → ? with |
---|
133 | [ DATA b2 ⇒ λ_. b2 |
---|
134 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) in |
---|
135 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with |
---|
136 | [ REGISTER r ⇒ λ_. |
---|
137 | [ ([[true; false; true; true; true]]) @@ r; b2; b3 ] |
---|
138 | | INDIRECT i1 ⇒ λ_. |
---|
139 | [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ] |
---|
140 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
141 | ] |
---|
142 | | DIV addr1 addr2 ⇒ |
---|
143 | [ ([[true;false;false;false;false;true;false;false]]) ] |
---|
144 | | INC addr ⇒ |
---|
145 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with |
---|
146 | [ ACC_A ⇒ λ_. |
---|
147 | [ ([[false;false;false;false;false;true;false;false]]) ] |
---|
148 | | REGISTER r ⇒ λ_. |
---|
149 | [ ([[false;false;false;false;true]]) @@ r ] |
---|
150 | | DIRECT b1 ⇒ λ_. |
---|
151 | [ ([[false; false; false; false; false; true; false; true]]); b1 ] |
---|
152 | | INDIRECT i1 ⇒ λ_. |
---|
153 | [ ([[false; false; false; false; false; true; true; i1]]) ] |
---|
154 | | DPTR ⇒ λ_. |
---|
155 | [ ([[true;false;true;false;false;false;true;true]]) ] |
---|
156 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
157 | | MOV addrs ⇒ |
---|
158 | match addrs with |
---|
159 | [ inl addrs ⇒ |
---|
160 | match addrs with |
---|
161 | [ inl addrs ⇒ |
---|
162 | match addrs with |
---|
163 | [ inl addrs ⇒ |
---|
164 | match addrs with |
---|
165 | [ inl addrs ⇒ |
---|
166 | match addrs with |
---|
167 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
168 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
169 | [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ] |
---|
170 | | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ] |
---|
171 | | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ] |
---|
172 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ] |
---|
173 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
174 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
175 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with |
---|
176 | [ REGISTER r ⇒ λ_. |
---|
177 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with |
---|
178 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ] |
---|
179 | | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ] |
---|
180 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ] |
---|
181 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
182 | | INDIRECT i1 ⇒ λ_. |
---|
183 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with |
---|
184 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ] |
---|
185 | | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ] |
---|
186 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ] |
---|
187 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
188 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
189 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
190 | let b1 ≝ |
---|
191 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
192 | [ DIRECT b1 ⇒ λ_. b1 |
---|
193 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
194 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with |
---|
195 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1] |
---|
196 | | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ] |
---|
197 | | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ] |
---|
198 | | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ] |
---|
199 | | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ] |
---|
200 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
201 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
202 | match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with |
---|
203 | [ DATA16 w ⇒ λ_. |
---|
204 | let b1_b2 ≝ split ? 8 8 w in |
---|
205 | let b1 ≝ \fst b1_b2 in |
---|
206 | let b2 ≝ \fst b1_b2 in |
---|
207 | [ ([[true;false;false;true;false;false;false;false]]); b1; b2] |
---|
208 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
209 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
210 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
211 | [ BIT_ADDR b1 ⇒ λ_. |
---|
212 | [ ([[true;false;true;false;false;false;true;false]]); b1 ] |
---|
213 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
214 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
215 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
216 | [ BIT_ADDR b1 ⇒ λ_. |
---|
217 | [ ([[true;false;false;true;false;false;true;false]]); b1 ] |
---|
218 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
219 | | MOVX addrs ⇒ |
---|
220 | match addrs with |
---|
221 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
222 | match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
223 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
224 | [ ([[true;true;true;false;false;false;true;i1]]) ] |
---|
225 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
226 | [ ([[true;true;true;false;false;false;false;false]]) ] |
---|
227 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
228 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
229 | match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
230 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
231 | [ ([[true;true;true;true;false;false;true;i1]]) ] |
---|
232 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
233 | [ ([[true;true;true;true;false;false;false;false]]) ] |
---|
234 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
235 | | MUL addr1 addr2 ⇒ |
---|
236 | [ ([[true;false;true;false;false;true;false;false]]) ] |
---|
237 | | NOP ⇒ |
---|
238 | [ ([[false;false;false;false;false;false;false;false]]) ] |
---|
239 | | ORL addrs ⇒ |
---|
240 | match addrs with |
---|
241 | [ inl addrs ⇒ |
---|
242 | match addrs with |
---|
243 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
244 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with |
---|
245 | [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ] |
---|
246 | | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ] |
---|
247 | | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ] |
---|
248 | | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ] |
---|
249 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
250 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
251 | let b1 ≝ |
---|
252 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
253 | [ DIRECT b1 ⇒ λ_. b1 |
---|
254 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
255 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
256 | [ ACC_A ⇒ λ_. |
---|
257 | [ ([[false;true;false;false;false;false;true;false]]); b1 ] |
---|
258 | | DATA b2 ⇒ λ_. |
---|
259 | [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ] |
---|
260 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
261 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
262 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with |
---|
263 | [ BIT_ADDR b1 ⇒ λ_. |
---|
264 | [ ([[false;true;true;true;false;false;true;false]]); b1 ] |
---|
265 | | N_BIT_ADDR b1 ⇒ λ_. |
---|
266 | [ ([[true;false;true;false;false;false;false;false]]); b1 ] |
---|
267 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
268 | | POP addr ⇒ |
---|
269 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
270 | [ DIRECT b1 ⇒ λ_. |
---|
271 | [ ([[true;true;false;true;false;false;false;false]]) ; b1 ] |
---|
272 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
273 | | PUSH addr ⇒ |
---|
274 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
275 | [ DIRECT b1 ⇒ λ_. |
---|
276 | [ ([[true;true;false;false;false;false;false;false]]) ; b1 ] |
---|
277 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
278 | | RET ⇒ |
---|
279 | [ ([[false;false;true;false;false;false;true;false]]) ] |
---|
280 | | RETI ⇒ |
---|
281 | [ ([[false;false;true;true;false;false;true;false]]) ] |
---|
282 | | RL addr ⇒ |
---|
283 | [ ([[false;false;true;false;false;false;true;true]]) ] |
---|
284 | | RLC addr ⇒ |
---|
285 | [ ([[false;false;true;true;false;false;true;true]]) ] |
---|
286 | | RR addr ⇒ |
---|
287 | [ ([[false;false;false;false;false;false;true;true]]) ] |
---|
288 | | RRC addr ⇒ |
---|
289 | [ ([[false;false;false;true;false;false;true;true]]) ] |
---|
290 | | SETB addr ⇒ |
---|
291 | match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with |
---|
292 | [ CARRY ⇒ λ_. |
---|
293 | [ ([[true;true;false;true;false;false;true;true]]) ] |
---|
294 | | BIT_ADDR b1 ⇒ λ_. |
---|
295 | [ ([[true;true;false;true;false;false;true;false]]); b1 ] |
---|
296 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
297 | | SUBB addr1 addr2 ⇒ |
---|
298 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
299 | [ REGISTER r ⇒ λ_. |
---|
300 | [ ([[true;false;false;true;true]]) @@ r ] |
---|
301 | | DIRECT b1 ⇒ λ_. |
---|
302 | [ ([[true;false;false;true;false;true;false;true]]); b1] |
---|
303 | | INDIRECT i1 ⇒ λ_. |
---|
304 | [ ([[true;false;false;true;false;true;true;i1]]) ] |
---|
305 | | DATA b1 ⇒ λ_. |
---|
306 | [ ([[true;false;false;true;false;true;false;false]]); b1] |
---|
307 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
308 | | SWAP addr ⇒ |
---|
309 | [ ([[true;true;false;false;false;true;false;false]]) ] |
---|
310 | | XCH addr1 addr2 ⇒ |
---|
311 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with |
---|
312 | [ REGISTER r ⇒ λ_. |
---|
313 | [ ([[true;true;false;false;true]]) @@ r ] |
---|
314 | | DIRECT b1 ⇒ λ_. |
---|
315 | [ ([[true;true;false;false;false;true;false;true]]); b1] |
---|
316 | | INDIRECT i1 ⇒ λ_. |
---|
317 | [ ([[true;true;false;false;false;true;true;i1]]) ] |
---|
318 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
319 | | XCHD addr1 addr2 ⇒ |
---|
320 | match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with |
---|
321 | [ INDIRECT i1 ⇒ λ_. |
---|
322 | [ ([[true;true;false;true;false;true;true;i1]]) ] |
---|
323 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
324 | | XRL addrs ⇒ |
---|
325 | match addrs with |
---|
326 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
327 | match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with |
---|
328 | [ REGISTER r ⇒ λ_. |
---|
329 | [ ([[false;true;true;false;true]]) @@ r ] |
---|
330 | | DIRECT b1 ⇒ λ_. |
---|
331 | [ ([[false;true;true;false;false;true;false;true]]); b1] |
---|
332 | | INDIRECT i1 ⇒ λ_. |
---|
333 | [ ([[false;true;true;false;false;true;true;i1]]) ] |
---|
334 | | DATA b1 ⇒ λ_. |
---|
335 | [ ([[false;true;true;false;false;true;false;false]]); b1] |
---|
336 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
337 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
338 | let b1 ≝ |
---|
339 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
340 | [ DIRECT b1 ⇒ λ_. b1 |
---|
341 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
342 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
343 | [ ACC_A ⇒ λ_. |
---|
344 | [ ([[false;true;true;false;false;false;true;false]]); b1 ] |
---|
345 | | DATA b2 ⇒ λ_. |
---|
346 | [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ] |
---|
347 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
348 | ]. |
---|
349 | |
---|
350 | definition assembly1 ≝ |
---|
351 | λi: instruction. |
---|
352 | match i with |
---|
353 | [ ACALL addr ⇒ |
---|
354 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
355 | [ ADDR11 w ⇒ λ_. |
---|
356 | let v1_v2 ≝ split ? 3 8 w in |
---|
357 | let v1 ≝ \fst v1_v2 in |
---|
358 | let v2 ≝ \snd v1_v2 in |
---|
359 | [ (v1 @@ [[true; false; false; false; true]]) ; v2 ] |
---|
360 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
361 | | AJMP addr ⇒ |
---|
362 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
363 | [ ADDR11 w ⇒ λ_. |
---|
364 | let v1_v2 ≝ split ? 3 8 w in |
---|
365 | let v1 ≝ \fst v1_v2 in |
---|
366 | let v2 ≝ \snd v1_v2 in |
---|
367 | [ (v1 @@ [[false; false; false; false; true]]) ; v2 ] |
---|
368 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
369 | | JMP adptr ⇒ |
---|
370 | [ ([[false;true;true;true;false;false;true;true]]) ] |
---|
371 | | LCALL addr ⇒ |
---|
372 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
---|
373 | [ ADDR16 w ⇒ λ_. |
---|
374 | let b1_b2 ≝ split ? 8 8 w in |
---|
375 | let b1 ≝ \fst b1_b2 in |
---|
376 | let b2 ≝ \snd b1_b2 in |
---|
377 | [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ] |
---|
378 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
379 | | LJMP addr ⇒ |
---|
380 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
---|
381 | [ ADDR16 w ⇒ λ_. |
---|
382 | let b1_b2 ≝ split ? 8 8 w in |
---|
383 | let b1 ≝ \fst b1_b2 in |
---|
384 | let b2 ≝ \snd b1_b2 in |
---|
385 | [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ] |
---|
386 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
387 | | MOVC addr1 addr2 ⇒ |
---|
388 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with |
---|
389 | [ ACC_DPTR ⇒ λ_. |
---|
390 | [ ([[true;false;false;true;false;false;true;true]]) ] |
---|
391 | | ACC_PC ⇒ λ_. |
---|
392 | [ ([[true;false;false;false;false;false;true;true]]) ] |
---|
393 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
394 | | SJMP addr ⇒ |
---|
395 | match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with |
---|
396 | [ RELATIVE b1 ⇒ λ_. |
---|
397 | [ ([[true;false;false;false;false;false;false;false]]); b1 ] |
---|
398 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
399 | | RealInstruction instr ⇒ |
---|
400 | assembly_preinstruction [[ relative ]] |
---|
401 | (λx. |
---|
402 | match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with |
---|
403 | [ RELATIVE r ⇒ λ_. r |
---|
404 | | _ ⇒ λabsd. ⊥ |
---|
405 | ] (subaddressing_modein … x)) instr |
---|
406 | ]. |
---|
407 | cases absd |
---|
408 | qed. |
---|
409 | |
---|
410 | definition is_relative_jump ≝ |
---|
411 | λi: preinstruction Identifier. |
---|
412 | match i with |
---|
413 | [ JC _ ⇒ True |
---|
414 | | JNC _ ⇒ True |
---|
415 | | JB _ _ ⇒ True |
---|
416 | | JNB _ _ ⇒ True |
---|
417 | | JBC _ _ ⇒ True |
---|
418 | | JZ _ ⇒ True |
---|
419 | | JNZ _ ⇒ True |
---|
420 | | CJNE _ _ ⇒ True |
---|
421 | | DJNZ _ _ ⇒ True |
---|
422 | | _ ⇒ False |
---|
423 | ]. |
---|
424 | |
---|
425 | definition pseudo_instruction_is_relative_jump: pseudo_instruction → Prop ≝ |
---|
426 | λi. |
---|
427 | match i with |
---|
428 | [ Instruction j ⇒ is_relative_jump j |
---|
429 | | _ ⇒ False ]. |
---|
430 | |
---|
431 | inductive jump_length: Type[0] ≝ |
---|
432 | | short_jump: jump_length |
---|
433 | | medium_jump: jump_length |
---|
434 | | long_jump: jump_length. |
---|
435 | |
---|
436 | definition jump_expansion_policy ≝ BitVectorTrie jump_length 16. |
---|
437 | |
---|
438 | definition find_right_call: Word → Word → nat × (option jump_length) ≝ |
---|
439 | (* medium call: 11 bits, only in "current segment" *) |
---|
440 | (* can this be done more efficiently with bit vector arithmetic? *) |
---|
441 | λpc.λaddress. |
---|
442 | let 〈p1, p2〉 ≝ split ? 5 11 address in |
---|
443 | let 〈a1, a2〉 ≝ split ? 5 11 pc in |
---|
444 | if eq_bv ? p1 a1 then (* we're in the same segment *) |
---|
445 | 〈2, Some ? medium_jump〉 |
---|
446 | else |
---|
447 | 〈3, Some ? long_jump〉. |
---|
448 | |
---|
449 | definition distance ≝ |
---|
450 | λx.λy.if gtb x y then x - y else y - x. |
---|
451 | |
---|
452 | definition find_right_jump: Word → Word → nat × (option jump_length) ≝ |
---|
453 | (* short jump: 8 bits signed *) |
---|
454 | (* medium jump: 11 bits, forward only *) |
---|
455 | λpc.λaddress. |
---|
456 | let pn ≝ nat_of_bitvector ? pc in |
---|
457 | let pa ≝ nat_of_bitvector ? address in |
---|
458 | if ltb (distance pn pa) 127 then |
---|
459 | 〈2, Some ? short_jump〉 |
---|
460 | else |
---|
461 | find_right_call pc address. |
---|
462 | |
---|
463 | definition find_right_relative_jump: Word → (BitVectorTrie Word 16) → |
---|
464 | Identifier → nat × (option jump_length) ≝ |
---|
465 | λpc.λlabels.λjmp. |
---|
466 | match lookup_opt ? ? jmp labels with |
---|
467 | [ None ⇒ 〈2, Some ? short_jump〉 |
---|
468 | | Some a ⇒ find_right_jump pc a ]. |
---|
469 | |
---|
470 | definition jep_relative: Word → (BitVectorTrie Word 16) → preinstruction Identifier → ? ≝ |
---|
471 | λpc.λlabels.λi. |
---|
472 | match i with |
---|
473 | [ JC jmp ⇒ find_right_relative_jump pc labels jmp |
---|
474 | | JNC jmp ⇒ find_right_relative_jump pc labels jmp |
---|
475 | | JB baddr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
476 | | JZ jmp ⇒ find_right_relative_jump pc labels jmp |
---|
477 | | JNZ jmp ⇒ find_right_relative_jump pc labels jmp |
---|
478 | | JBC baddr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
479 | | JNB baddr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
480 | | CJNE addr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
481 | | DJNZ addr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
482 | | _ ⇒ 〈length ? (assembly_preinstruction ? (λx.zero ?) i), None …〉 ]. |
---|
483 | |
---|
484 | definition jump_expansion_policy_internal: pseudo_assembly_program → |
---|
485 | (BitVectorTrie Word 16) → jump_expansion_policy → |
---|
486 | ((BitVectorTrie Word 16) × jump_expansion_policy × bool)≝ |
---|
487 | λprogram.λlabels.λpolicy. |
---|
488 | let 〈pc, new_labels, new_policy, changed〉 ≝ |
---|
489 | foldl ? ? (λacc.λi:labelled_instruction. |
---|
490 | let 〈label, instr〉 ≝ i in |
---|
491 | let 〈pc,labels,policy,c0〉 ≝ acc in |
---|
492 | let 〈c1,new_labels〉 ≝ match label with |
---|
493 | [ None ⇒ 〈c0,labels〉 |
---|
494 | | Some l ⇒ |
---|
495 | match update ? ? pc l labels with |
---|
496 | [ None ⇒ 〈c0,labels〉 |
---|
497 | | Some x ⇒ 〈true, x〉 ] ] in |
---|
498 | let 〈pc_delta, jmp_len〉 ≝ match instr with |
---|
499 | [ Call c ⇒ |
---|
500 | match lookup_opt ? ? c new_labels with |
---|
501 | [ None ⇒ 〈2, Some ? medium_jump〉 |
---|
502 | | Some c_addr ⇒ find_right_call pc c_addr ] |
---|
503 | | Jmp j ⇒ |
---|
504 | match lookup_opt ? ? j new_labels with |
---|
505 | [ None ⇒ 〈2, Some ? short_jump〉 |
---|
506 | | Some j_addr ⇒ find_right_jump pc j_addr ] |
---|
507 | | Instruction i ⇒ jep_relative pc new_labels i |
---|
508 | | Mov _ _ ⇒ 〈3, None …〉 |
---|
509 | | Cost _ ⇒ 〈0, None …〉 |
---|
510 | | Comment _ ⇒ 〈0, None …〉 ] in |
---|
511 | let 〈new_pc,ignore〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in |
---|
512 | match jmp_len with |
---|
513 | [ None ⇒ 〈new_pc, new_labels, policy, c1〉 |
---|
514 | | Some j ⇒ |
---|
515 | match update ? ? pc j policy with |
---|
516 | [ None ⇒ 〈new_pc, new_labels, policy, c1〉 |
---|
517 | | Some x ⇒ 〈new_pc, new_labels, x, true〉 ] ] |
---|
518 | ) 〈zero ?, labels, policy, false〉 (\snd program) in |
---|
519 | 〈labels, policy, changed〉. |
---|
520 | |
---|
521 | definition expand_relative_jump_internal: |
---|
522 | (Identifier → Word) → jump_length → Identifier → Word → ([[relative]] → preinstruction [[relative]]) → |
---|
523 | option (list instruction) |
---|
524 | ≝ |
---|
525 | λlookup_labels,jmp_len.λjmp:Identifier.λpc,i. |
---|
526 | match jmp_len with |
---|
527 | [ short_jump ⇒ |
---|
528 | let lookup_address ≝ lookup_labels jmp in |
---|
529 | let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in |
---|
530 | let 〈upper, lower〉 ≝ split ? 8 8 result in |
---|
531 | if eq_bv ? upper (zero 8) then |
---|
532 | let address ≝ RELATIVE lower in |
---|
533 | Some ? [ RealInstruction (i address) ] |
---|
534 | else |
---|
535 | None ? |
---|
536 | | medium_jump ⇒ None … |
---|
537 | | long_jump ⇒ |
---|
538 | Some ? |
---|
539 | [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2))); |
---|
540 | SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *) |
---|
541 | LJMP (ADDR16 (lookup_labels jmp)) |
---|
542 | ] |
---|
543 | ]. |
---|
544 | @ I |
---|
545 | qed. |
---|
546 | |
---|
547 | definition expand_relative_jump: (Identifier → Word) → jump_length → Word → preinstruction Identifier → option (list instruction) ≝ |
---|
548 | λlookup_labels. |
---|
549 | λjmp_len: jump_length. |
---|
550 | λpc. |
---|
551 | λi: preinstruction Identifier. |
---|
552 | let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in |
---|
553 | match i with |
---|
554 | [ JC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JC ?) |
---|
555 | | JNC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNC ?) |
---|
556 | | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JB ? baddr) |
---|
557 | | JZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JZ ?) |
---|
558 | | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNZ ?) |
---|
559 | | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JBC ? baddr) |
---|
560 | | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNB ? baddr) |
---|
561 | | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (CJNE ? addr) |
---|
562 | | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (DJNZ ? addr) |
---|
563 | | ADD arg1 arg2 ⇒ Some ? [ ADD ? arg1 arg2 ] |
---|
564 | | ADDC arg1 arg2 ⇒ Some ? [ ADDC ? arg1 arg2 ] |
---|
565 | | SUBB arg1 arg2 ⇒ Some ? [ SUBB ? arg1 arg2 ] |
---|
566 | | INC arg ⇒ Some ? [ INC ? arg ] |
---|
567 | | DEC arg ⇒ Some ? [ DEC ? arg ] |
---|
568 | | MUL arg1 arg2 ⇒ Some ? [ MUL ? arg1 arg2 ] |
---|
569 | | DIV arg1 arg2 ⇒ Some ? [ DIV ? arg1 arg2 ] |
---|
570 | | DA arg ⇒ Some ? [ DA ? arg ] |
---|
571 | | ANL arg ⇒ Some ? [ ANL ? arg ] |
---|
572 | | ORL arg ⇒ Some ? [ ORL ? arg ] |
---|
573 | | XRL arg ⇒ Some ? [ XRL ? arg ] |
---|
574 | | CLR arg ⇒ Some ? [ CLR ? arg ] |
---|
575 | | CPL arg ⇒ Some ? [ CPL ? arg ] |
---|
576 | | RL arg ⇒ Some ? [ RL ? arg ] |
---|
577 | | RR arg ⇒ Some ? [ RR ? arg ] |
---|
578 | | RLC arg ⇒ Some ? [ RLC ? arg ] |
---|
579 | | RRC arg ⇒ Some ? [ RRC ? arg ] |
---|
580 | | SWAP arg ⇒ Some ? [ SWAP ? arg ] |
---|
581 | | MOV arg ⇒ Some ? [ MOV ? arg ] |
---|
582 | | MOVX arg ⇒ Some ? [ MOVX ? arg ] |
---|
583 | | SETB arg ⇒ Some ? [ SETB ? arg ] |
---|
584 | | PUSH arg ⇒ Some ? [ PUSH ? arg ] |
---|
585 | | POP arg ⇒ Some ? [ POP ? arg ] |
---|
586 | | XCH arg1 arg2 ⇒ Some ? [ XCH ? arg1 arg2 ] |
---|
587 | | XCHD arg1 arg2 ⇒ Some ? [ XCHD ? arg1 arg2 ] |
---|
588 | | RET ⇒ Some ? [ RET ? ] |
---|
589 | | RETI ⇒ Some ? [ RETI ? ] |
---|
590 | | NOP ⇒ Some ? [ RealInstruction (NOP ?) ] |
---|
591 | ]. |
---|
592 | |
---|
593 | definition expand_pseudo_instruction: ? → ? → Word → jump_length → pseudo_instruction → option (list instruction) ≝ |
---|
594 | λlookup_labels. |
---|
595 | λlookup_datalabels. |
---|
596 | λpc. |
---|
597 | λjmp_len. |
---|
598 | λi. |
---|
599 | match i with |
---|
600 | [ Cost cost ⇒ Some ? [ ] |
---|
601 | | Comment comment ⇒ Some ? [ ] |
---|
602 | | Call call ⇒ |
---|
603 | match jmp_len with |
---|
604 | [ short_jump ⇒ None ? |
---|
605 | | medium_jump ⇒ |
---|
606 | let 〈ignore, address〉 ≝ split ? 5 11 (lookup_labels call) in |
---|
607 | let 〈fst_5, rest〉 ≝ split ? 5 11 pc in |
---|
608 | if eq_bv ? ignore fst_5 then |
---|
609 | let address ≝ ADDR11 address in |
---|
610 | Some ? ([ ACALL address ]) |
---|
611 | else |
---|
612 | None ? |
---|
613 | | long_jump ⇒ |
---|
614 | let address ≝ ADDR16 (lookup_labels call) in |
---|
615 | Some ? [ LCALL address ] |
---|
616 | ] |
---|
617 | | Mov d trgt ⇒ |
---|
618 | let address ≝ DATA16 (lookup_datalabels trgt) in |
---|
619 | Some ? [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))] |
---|
620 | | Instruction instr ⇒ expand_relative_jump lookup_labels jmp_len pc instr |
---|
621 | | Jmp jmp ⇒ |
---|
622 | match jmp_len with |
---|
623 | [ short_jump ⇒ |
---|
624 | let lookup_address ≝ lookup_labels jmp in |
---|
625 | let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in |
---|
626 | let 〈upper, lower〉 ≝ split ? 8 8 result in |
---|
627 | if eq_bv ? upper (zero 8) then |
---|
628 | let address ≝ RELATIVE lower in |
---|
629 | Some ? [ SJMP address ] |
---|
630 | else |
---|
631 | None ? |
---|
632 | | medium_jump ⇒ |
---|
633 | let address ≝ lookup_labels jmp in |
---|
634 | let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 address in |
---|
635 | let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in |
---|
636 | if eq_bv ? fst_5_addr fst_5_pc then |
---|
637 | let address ≝ ADDR11 rest_addr in |
---|
638 | Some ? ([ AJMP address ]) |
---|
639 | else |
---|
640 | None ? |
---|
641 | | long_jump ⇒ |
---|
642 | let address ≝ ADDR16 (lookup_labels jmp) in |
---|
643 | Some ? [ LJMP address ] |
---|
644 | ] |
---|
645 | ]. |
---|
646 | @ I |
---|
647 | qed. |
---|
648 | |
---|
649 | let rec jump_expansion_internal (n: nat) (program: pseudo_assembly_program) |
---|
650 | (old_labels: BitVectorTrie Word 16) (old_policy: BitVectorTrie jump_length 16) |
---|
651 | on n: jump_expansion_policy ≝ |
---|
652 | match n with |
---|
653 | [ O ⇒ old_policy |
---|
654 | | S n' ⇒ |
---|
655 | let 〈new_labels, new_policy, ch〉 ≝ |
---|
656 | jump_expansion_policy_internal program old_labels old_policy in |
---|
657 | jump_expansion_internal n' program new_labels new_policy ]. |
---|
658 | |
---|
659 | (**************************************** START OF POLICY ABSTRACTION ********************) |
---|
660 | |
---|
661 | definition policy_type ≝ Word → jump_length. |
---|
662 | |
---|
663 | definition jump_expansion': pseudo_assembly_program → policy_type ≝ |
---|
664 | λprogram.λpc. |
---|
665 | let policy ≝ jump_expansion_internal (length ? (\snd program)) program (Stub ? ?) (Stub ? ?) in |
---|
666 | lookup ? ? pc policy long_jump. |
---|
667 | |
---|
668 | definition assembly_1_pseudoinstruction_safe ≝ |
---|
669 | λprogram: pseudo_assembly_program. |
---|
670 | λjump_expansion: policy_type. |
---|
671 | λppc: Word. |
---|
672 | λpc: Word. |
---|
673 | λlookup_labels. |
---|
674 | λlookup_datalabels. |
---|
675 | λi. |
---|
676 | let expansion ≝ jump_expansion ppc in |
---|
677 | match expand_pseudo_instruction lookup_labels lookup_datalabels pc expansion i with |
---|
678 | [ None ⇒ None ? |
---|
679 | | Some pseudos ⇒ |
---|
680 | let mapped ≝ map ? ? assembly1 pseudos in |
---|
681 | let flattened ≝ flatten ? mapped in |
---|
682 | let pc_len ≝ length ? flattened in |
---|
683 | Some ? (〈pc_len, flattened〉) |
---|
684 | ]. |
---|
685 | |
---|
686 | definition construct_costs_safe ≝ |
---|
687 | λprogram. |
---|
688 | λjump_expansion: policy_type. |
---|
689 | λpseudo_program_counter: nat. |
---|
690 | λprogram_counter: nat. |
---|
691 | λcosts: BitVectorTrie Word 16. |
---|
692 | λi. |
---|
693 | match i with |
---|
694 | [ Cost cost ⇒ |
---|
695 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
696 | Some ? 〈program_counter, (insert ? ? program_counter_bv cost costs)〉 |
---|
697 | | _ ⇒ |
---|
698 | let pc_bv ≝ bitvector_of_nat ? program_counter in |
---|
699 | let ppc_bv ≝ bitvector_of_nat ? pseudo_program_counter in |
---|
700 | let lookup_labels ≝ λx.pc_bv in |
---|
701 | let lookup_datalabels ≝ λx.zero ? in |
---|
702 | let pc_delta_assembled ≝ |
---|
703 | assembly_1_pseudoinstruction_safe program jump_expansion ppc_bv pc_bv |
---|
704 | lookup_labels lookup_datalabels i |
---|
705 | in |
---|
706 | match pc_delta_assembled with |
---|
707 | [ None ⇒ None ? |
---|
708 | | Some pc_delta_assembled ⇒ |
---|
709 | let 〈pc_delta, assembled〉 ≝ pc_delta_assembled in |
---|
710 | Some ? 〈pc_delta + program_counter, costs〉 |
---|
711 | ] |
---|
712 | ]. |
---|
713 | |
---|
714 | (* This establishes the correspondence between pseudo program counters and |
---|
715 | program counters. It is at the heart of the proof. *) |
---|
716 | (*CSC: code taken from build_maps *) |
---|
717 | definition sigma00: pseudo_assembly_program → policy_type → list ? → option (nat × (nat × (BitVectorTrie Word 16))) ≝ |
---|
718 | λinstr_list. |
---|
719 | λjump_expansion: policy_type. |
---|
720 | λl:list labelled_instruction. |
---|
721 | foldl ?? |
---|
722 | (λt,i. |
---|
723 | match t with |
---|
724 | [ None ⇒ None ? |
---|
725 | | Some ppc_pc_map ⇒ |
---|
726 | let 〈ppc,pc_map〉 ≝ ppc_pc_map in |
---|
727 | let 〈program_counter, sigma_map〉 ≝ pc_map in |
---|
728 | let 〈label, i〉 ≝ i in |
---|
729 | match construct_costs_safe instr_list jump_expansion ppc program_counter (Stub …) i with |
---|
730 | [ None ⇒ None ? |
---|
731 | | Some pc_ignore ⇒ |
---|
732 | let 〈pc,ignore〉 ≝ pc_ignore in |
---|
733 | Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ] |
---|
734 | ]) (Some ? 〈0, 〈0, (Stub ? ?)〉〉) l. |
---|
735 | |
---|
736 | definition sigma0: pseudo_assembly_program → policy_type → option (nat × (nat × (BitVectorTrie Word 16))) |
---|
737 | ≝ λprog.λjump_expansion.sigma00 prog jump_expansion (\snd prog). |
---|
738 | |
---|
739 | definition tech_pc_sigma00: pseudo_assembly_program → policy_type → list labelled_instruction → option (nat × nat) ≝ |
---|
740 | λprogram,jump_expansion,instr_list. |
---|
741 | match sigma00 program jump_expansion instr_list with |
---|
742 | [ None ⇒ None … |
---|
743 | | Some result ⇒ |
---|
744 | let 〈ppc,pc_sigma_map〉 ≝ result in |
---|
745 | let 〈pc,map〉 ≝ pc_sigma_map in |
---|
746 | Some … 〈ppc,pc〉 ]. |
---|
747 | |
---|
748 | definition sigma_safe: pseudo_assembly_program → policy_type → option (Word → Word) ≝ |
---|
749 | λinstr_list,jump_expansion. |
---|
750 | match sigma0 instr_list jump_expansion with |
---|
751 | [ None ⇒ None ? |
---|
752 | | Some result ⇒ |
---|
753 | let 〈ppc,pc_sigma_map〉 ≝ result in |
---|
754 | let 〈pc, sigma_map〉 ≝ pc_sigma_map in |
---|
755 | if gtb pc (2^16) then |
---|
756 | None ? |
---|
757 | else |
---|
758 | Some ? (λx.lookup ?? x sigma_map (zero …)) ]. |
---|
759 | |
---|
760 | (* stuff about policy *) |
---|
761 | |
---|
762 | definition policy_ok ≝ λjump_expansion,p. sigma_safe p jump_expansion ≠ None …. |
---|
763 | |
---|
764 | definition policy ≝ λp. Σjump_expansion:policy_type. policy_ok jump_expansion p. |
---|
765 | |
---|
766 | lemma eject_policy: ∀p. policy p → policy_type. |
---|
767 | #p #pol @(eject … pol) |
---|
768 | qed. |
---|
769 | |
---|
770 | coercion eject_policy nocomposites: ∀p.∀pol:policy p. policy_type ≝ eject_policy on _pol:(policy ?) to policy_type. |
---|
771 | |
---|
772 | definition sigma: ∀p:pseudo_assembly_program. policy p → Word → Word ≝ |
---|
773 | λp,policy. |
---|
774 | match sigma_safe p (eject … policy) return λr:option (Word → Word). r ≠ None … → Word → Word with |
---|
775 | [ None ⇒ λabs. ⊥ |
---|
776 | | Some r ⇒ λ_.r] (sig2 … policy). |
---|
777 | cases abs /2/ |
---|
778 | qed. |
---|
779 | |
---|
780 | example sigma_0: ∀p,pol. sigma p pol (bitvector_of_nat ? 0) = bitvector_of_nat ? 0. |
---|
781 | cases daemon. |
---|
782 | qed. |
---|
783 | |
---|
784 | (* MAIN AXIOM HERE, HIDDEN USING cases daemon *) |
---|
785 | definition assembly_1_pseudoinstruction: |
---|
786 | ∀program:pseudo_assembly_program.∀pol: policy program. |
---|
787 | ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi. |
---|
788 | lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) → |
---|
789 | lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)) → |
---|
790 | \fst (fetch_pseudo_instruction (\snd program) ppc) = pi → |
---|
791 | Σres:(nat × (list Byte)). |
---|
792 | let 〈len,code〉 ≝ res in |
---|
793 | sigma program pol (\snd (half_add ? ppc (bitvector_of_nat ? 1))) = |
---|
794 | bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len) |
---|
795 | ≝ λprogram: pseudo_assembly_program. |
---|
796 | λpol: policy program. |
---|
797 | λppc: Word. |
---|
798 | λlookup_labels. |
---|
799 | λlookup_datalabels. |
---|
800 | λpi. |
---|
801 | λprf1,prf2,prf3. |
---|
802 | match assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi with |
---|
803 | [ None ⇒ let dummy ≝ 〈0,[ ]〉 in dummy |
---|
804 | | Some res ⇒ res ]. |
---|
805 | [ @⊥ elim pi in p [*] |
---|
806 | try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs |
---|
807 | generalize in match (jmeq_to_eq ??? abs) -abs; #abs whd in abs:(??%?); try destruct(abs) |
---|
808 | whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?); |
---|
809 | (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *) |
---|
810 | cases daemon |
---|
811 | | cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta; |
---|
812 | (* THIS SHOULD BE TRUE INSTEAD *) |
---|
813 | cases daemon] |
---|
814 | qed. |
---|
815 | |
---|
816 | (* MAIN AXIOM HERE, HIDDEN USING cases daemon *) |
---|
817 | definition construct_costs': |
---|
818 | ∀program. policy program → nat → nat → ? → ? → |
---|
819 | Σres:(nat × (BitVectorTrie Word 16)). True |
---|
820 | ≝ |
---|
821 | λprogram.λpol: policy program.λppc,pc,costs,i. |
---|
822 | match construct_costs_safe program pol ppc pc costs i with |
---|
823 | [ None ⇒ let dummy ≝ 〈0, Stub ??〉 in dummy |
---|
824 | | Some res ⇒ res ]. |
---|
825 | [ cases daemon |
---|
826 | | %] |
---|
827 | qed. |
---|
828 | |
---|
829 | definition construct_costs ≝ |
---|
830 | λprogram,pol,ppc,pc,costs,i. eject … (construct_costs' program pol ppc pc costs i). |
---|
831 | |
---|
832 | axiom construct_costs_sigma: |
---|
833 | ∀p.∀pol:policy p.∀ppc,pc,costs,i. |
---|
834 | bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) → |
---|
835 | bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)). |
---|
836 | |
---|
837 | axiom tech_pc_sigma00_append_Some: |
---|
838 | ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc. |
---|
839 | tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 → |
---|
840 | tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉. |
---|
841 | |
---|
842 | definition build_maps: |
---|
843 | ∀pseudo_program.∀pol:policy pseudo_program. |
---|
844 | Σres:((BitVectorTrie Word 16) × (BitVectorTrie Word 16)). |
---|
845 | let 〈labels,costs〉 ≝ res in |
---|
846 | ∀id. occurs_exactly_once id (\snd pseudo_program) → |
---|
847 | lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem (\snd pseudo_program) id) |
---|
848 | ≝ |
---|
849 | λpseudo_program.λpol:policy pseudo_program. |
---|
850 | let result ≝ |
---|
851 | foldl_strong |
---|
852 | (option Identifier × pseudo_instruction) |
---|
853 | (λpre. Σres:((BitVectorTrie Word 16) × (nat × (nat × (BitVectorTrie Word 16)))). |
---|
854 | let 〈labels,ppc_pc_costs〉 ≝ res in |
---|
855 | let 〈ppc',pc_costs〉 ≝ ppc_pc_costs in |
---|
856 | let 〈pc',costs〉 ≝ pc_costs in |
---|
857 | And ( And ( |
---|
858 | And (bitvector_of_nat ? pc' = sigma pseudo_program pol (bitvector_of_nat ? ppc')) (*∧*) |
---|
859 | (ppc' = length … pre)) (*∧ *) |
---|
860 | (tech_pc_sigma00 pseudo_program (eject … pol) pre = Some ? 〈ppc',pc'〉)) (*∧*) |
---|
861 | (∀id. occurs_exactly_once id pre → |
---|
862 | lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem pre id))) |
---|
863 | (\snd pseudo_program) |
---|
864 | (λprefix,i,tl,prf,t. |
---|
865 | let 〈labels, ppc_pc_costs〉 ≝ t in |
---|
866 | let 〈ppc, pc_costs〉 ≝ ppc_pc_costs in |
---|
867 | let 〈pc,costs〉 ≝ pc_costs in |
---|
868 | let 〈label, i'〉 ≝ i in |
---|
869 | let labels ≝ |
---|
870 | match label with |
---|
871 | [ None ⇒ labels |
---|
872 | | Some label ⇒ |
---|
873 | let program_counter_bv ≝ bitvector_of_nat ? pc in |
---|
874 | insert ? ? label program_counter_bv labels] |
---|
875 | in |
---|
876 | let construct ≝ construct_costs pseudo_program pol ppc pc costs i' in |
---|
877 | 〈labels, 〈S ppc,construct〉〉 |
---|
878 | ) 〈(Stub ? ?), 〈0, 〈0, Stub ? ?〉〉〉 |
---|
879 | in |
---|
880 | let 〈labels, ppc_pc_costs〉 ≝ result in |
---|
881 | let 〈ppc,pc_costs〉 ≝ ppc_pc_costs in |
---|
882 | let 〈pc, costs〉 ≝ pc_costs in |
---|
883 | 〈labels, costs〉. |
---|
884 | [2: whd generalize in match (sig2 … t) >p >p1 >p2 >p3 * * * #IHn1 #IH0 #IH1 #IH2 |
---|
885 | generalize in match (refl … construct); cases construct in ⊢ (???% → %) #PC #CODE #JMEQ % [% [%]] |
---|
886 | [ <(construct_costs_sigma … costs i1 IHn1) change with (? = ?(\fst construct)) >JMEQ % |
---|
887 | | >length_append <IH0 normalize; -IHn1; (*CSC: otherwise it diverges!*) // |
---|
888 | | >(tech_pc_sigma00_append_Some … costs … IH1) change with (Some … 〈S ppc,\fst construct〉 = ?) >JMEQ % |
---|
889 | | #id normalize nodelta; -labels1; cases label; normalize nodelta |
---|
890 | [ #K <address_of_word_labels_code_mem_None [2:@K] @IH2 -IHn1; (*CSC: otherwise it diverges!*) // |
---|
891 | | #l #H generalize in match (occurs_exactly_once_Some ???? H) in ⊢ ?; |
---|
892 | generalize in match (refl … (eq_bv ? l id)); cases (eq_bv … l id) in ⊢ (???% → %) |
---|
893 | [ #EQ #_ <(eq_bv_eq … EQ) >lookup_insert_hit >address_of_word_labels_code_mem_Some_hit |
---|
894 | <IH0 [@IHn1 | <(eq_bv_eq … EQ) in H #H @H] |
---|
895 | | #EQ change with (occurs_exactly_once ?? → ?) #K >lookup_insert_miss [2: -IHn1; /2/] |
---|
896 | <(address_of_word_labels_code_mem_Some_miss … EQ … H) @IH2 -IHn1; //]]] |
---|
897 | |3: whd % [% [%]] // [>sigma_0//] #id normalize in ⊢ (% → ?) #abs @⊥ // |
---|
898 | | generalize in match (sig2 … result) in ⊢ ?; |
---|
899 | normalize nodelta in p ⊢ %; -result; >p in ⊢ (match % with [_ ⇒ ?] → ?) |
---|
900 | normalize nodelta; >p1 normalize nodelta; >p2; normalize nodelta; * #_; #H @H] |
---|
901 | qed. |
---|
902 | |
---|
903 | definition assembly: |
---|
904 | ∀p:pseudo_assembly_program. policy p → list Byte × (BitVectorTrie Identifier 16) ≝ |
---|
905 | λp.let 〈preamble, instr_list〉 ≝ p in |
---|
906 | λpol. |
---|
907 | let 〈labels,costs〉 ≝ build_maps 〈preamble,instr_list〉 pol in |
---|
908 | let datalabels ≝ construct_datalabels preamble in |
---|
909 | let lookup_labels ≝ λx. lookup ? ? x labels (zero ?) in |
---|
910 | let lookup_datalabels ≝ λx. lookup ? ? x datalabels (zero ?) in |
---|
911 | let result ≝ |
---|
912 | foldl_strong |
---|
913 | (option Identifier × pseudo_instruction) |
---|
914 | (λpre. Σpc_ppc_code:(Word × (Word × (list Byte))). |
---|
915 | let 〈pc,ppc_code〉 ≝ pc_ppc_code in |
---|
916 | let 〈ppc,code〉 ≝ ppc_code in |
---|
917 | ∀ppc'. |
---|
918 | let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' in |
---|
919 | let 〈len,assembledi〉 ≝ |
---|
920 | assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc' lookup_labels lookup_datalabels pi ??? in |
---|
921 | True) |
---|
922 | instr_list |
---|
923 | (λprefix,hd,tl,prf,pc_ppc_code. |
---|
924 | let 〈pc, ppc_code〉 ≝ pc_ppc_code in |
---|
925 | let 〈ppc, code〉 ≝ ppc_code in |
---|
926 | let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc lookup_labels lookup_datalabels (\snd hd) ??? in |
---|
927 | let 〈new_pc, flags〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in |
---|
928 | let new_ppc ≝ \snd (half_add ? ppc (bitvector_of_nat ? 1)) in |
---|
929 | 〈new_pc, 〈new_ppc, (code @ program)〉〉) |
---|
930 | 〈(zero ?), 〈(zero ?), [ ]〉〉 |
---|
931 | in |
---|
932 | 〈\snd (\snd result), costs〉. |
---|
933 | [2,5: % |
---|
934 | |*: cases daemon ] |
---|
935 | qed. |
---|
936 | |
---|
937 | definition assembly_unlabelled_program: assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝ |
---|
938 | λp. Some ? (〈foldr ? ? (λi,l. assembly1 i @ l) [ ] p, Stub …〉). |
---|