[719] | 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 | |
---|
[820] | 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 ⇒ |
---|
[757] | 13 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 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 ⇒ |
---|
[757] | 20 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 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 |
---|
[757] | 30 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 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 ⇒ |
---|
[757] | 72 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with |
---|
[719] | 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) |
---|
[820] | 82 | | DJNZ addr1 addr2 ⇒ |
---|
| 83 | let b2 ≝ addr_of addr2 in |
---|
[757] | 84 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with |
---|
[719] | 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 ⇒ |
---|
[820] | 91 | let b1 ≝ addr_of addr in |
---|
| 92 | [ ([[false; true; false; false; false; false; false; false]]); b1 ] |
---|
[719] | 93 | | JNC addr ⇒ |
---|
[820] | 94 | let b1 ≝ addr_of addr in |
---|
| 95 | [ ([[false; true; false; true; false; false; false; false]]); b1 ] |
---|
[719] | 96 | | JZ addr ⇒ |
---|
[820] | 97 | let b1 ≝ addr_of addr in |
---|
| 98 | [ ([[false; true; true; false; false; false; false; false]]); b1 ] |
---|
[719] | 99 | | JNZ addr ⇒ |
---|
[820] | 100 | let b1 ≝ addr_of addr in |
---|
| 101 | [ ([[false; true; true; true; false; false; false; false]]); b1 ] |
---|
[719] | 102 | | JB addr1 addr2 ⇒ |
---|
[820] | 103 | let b2 ≝ addr_of addr2 in |
---|
[719] | 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 ⇒ |
---|
[820] | 109 | let b2 ≝ addr_of addr2 in |
---|
[719] | 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 ⇒ |
---|
[820] | 115 | let b2 ≝ addr_of addr2 in |
---|
[719] | 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 ⇒ |
---|
[820] | 121 | let b3 ≝ addr_of addr3 in |
---|
[719] | 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 |
---|
[757] | 135 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with |
---|
[820] | 136 | [ REGISTER r ⇒ λ_. |
---|
[719] | 137 | [ ([[true; false; true; true; true]]) @@ r; b2; b3 ] |
---|
| 138 | | INDIRECT i1 ⇒ λ_. |
---|
| 139 | [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ] |
---|
[820] | 140 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
| 141 | ] |
---|
| 142 | | DIV addr1 addr2 ⇒ |
---|
| 143 | [ ([[true;false;false;false;false;true;false;false]]) ] |
---|
[719] | 144 | | INC addr ⇒ |
---|
[757] | 145 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with |
---|
[719] | 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 |
---|
[757] | 168 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 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 |
---|
[757] | 175 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with |
---|
[719] | 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 |
---|
[757] | 194 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with |
---|
[719] | 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 | [ ([[true;false;false;true;false;false;false;false]]); b1; b2] |
---|
| 206 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 207 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 208 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 209 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 210 | [ ([[true;false;true;false;false;false;true;false]]); b1 ] |
---|
| 211 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 212 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 213 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 214 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 215 | [ ([[true;false;false;true;false;false;true;false]]); b1 ] |
---|
| 216 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 217 | | MOVX addrs ⇒ |
---|
| 218 | match addrs with |
---|
| 219 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 220 | match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
| 221 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
| 222 | [ ([[true;true;true;false;false;false;true;i1]]) ] |
---|
| 223 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
| 224 | [ ([[true;true;true;false;false;false;false;false]]) ] |
---|
| 225 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 226 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 227 | match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
| 228 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
| 229 | [ ([[true;true;true;true;false;false;true;i1]]) ] |
---|
| 230 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
| 231 | [ ([[true;true;true;true;false;false;false;false]]) ] |
---|
| 232 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 233 | | MUL addr1 addr2 ⇒ |
---|
| 234 | [ ([[true;false;true;false;false;true;false;false]]) ] |
---|
| 235 | | NOP ⇒ |
---|
| 236 | [ ([[false;false;false;false;false;false;false;false]]) ] |
---|
| 237 | | ORL addrs ⇒ |
---|
| 238 | match addrs with |
---|
| 239 | [ inl addrs ⇒ |
---|
| 240 | match addrs with |
---|
| 241 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 242 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with |
---|
[719] | 243 | [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ] |
---|
| 244 | | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ] |
---|
| 245 | | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ] |
---|
| 246 | | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ] |
---|
| 247 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 248 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 249 | let b1 ≝ |
---|
| 250 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 251 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 252 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 253 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 254 | [ ACC_A ⇒ λ_. |
---|
| 255 | [ ([[false;true;false;false;false;false;true;false]]); b1 ] |
---|
| 256 | | DATA b2 ⇒ λ_. |
---|
| 257 | [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ] |
---|
| 258 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 259 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 260 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with |
---|
| 261 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 262 | [ ([[false;true;true;true;false;false;true;false]]); b1 ] |
---|
| 263 | | N_BIT_ADDR b1 ⇒ λ_. |
---|
| 264 | [ ([[true;false;true;false;false;false;false;false]]); b1 ] |
---|
| 265 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 266 | | POP addr ⇒ |
---|
| 267 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 268 | [ DIRECT b1 ⇒ λ_. |
---|
| 269 | [ ([[true;true;false;true;false;false;false;false]]) ; b1 ] |
---|
| 270 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 271 | | PUSH addr ⇒ |
---|
| 272 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 273 | [ DIRECT b1 ⇒ λ_. |
---|
| 274 | [ ([[true;true;false;false;false;false;false;false]]) ; b1 ] |
---|
| 275 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 276 | | RET ⇒ |
---|
| 277 | [ ([[false;false;true;false;false;false;true;false]]) ] |
---|
| 278 | | RETI ⇒ |
---|
| 279 | [ ([[false;false;true;true;false;false;true;false]]) ] |
---|
| 280 | | RL addr ⇒ |
---|
| 281 | [ ([[false;false;true;false;false;false;true;true]]) ] |
---|
| 282 | | RLC addr ⇒ |
---|
| 283 | [ ([[false;false;true;true;false;false;true;true]]) ] |
---|
| 284 | | RR addr ⇒ |
---|
| 285 | [ ([[false;false;false;false;false;false;true;true]]) ] |
---|
| 286 | | RRC addr ⇒ |
---|
| 287 | [ ([[false;false;false;true;false;false;true;true]]) ] |
---|
| 288 | | SETB addr ⇒ |
---|
| 289 | match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with |
---|
| 290 | [ CARRY ⇒ λ_. |
---|
| 291 | [ ([[true;true;false;true;false;false;true;true]]) ] |
---|
| 292 | | BIT_ADDR b1 ⇒ λ_. |
---|
| 293 | [ ([[true;true;false;true;false;false;true;false]]); b1 ] |
---|
| 294 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 295 | | SUBB addr1 addr2 ⇒ |
---|
[757] | 296 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 297 | [ REGISTER r ⇒ λ_. |
---|
| 298 | [ ([[true;false;false;true;true]]) @@ r ] |
---|
| 299 | | DIRECT b1 ⇒ λ_. |
---|
| 300 | [ ([[true;false;false;true;false;true;false;true]]); b1] |
---|
| 301 | | INDIRECT i1 ⇒ λ_. |
---|
| 302 | [ ([[true;false;false;true;false;true;true;i1]]) ] |
---|
| 303 | | DATA b1 ⇒ λ_. |
---|
| 304 | [ ([[true;false;false;true;false;true;false;false]]); b1] |
---|
| 305 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 306 | | SWAP addr ⇒ |
---|
| 307 | [ ([[true;true;false;false;false;true;false;false]]) ] |
---|
| 308 | | XCH addr1 addr2 ⇒ |
---|
[757] | 309 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with |
---|
[719] | 310 | [ REGISTER r ⇒ λ_. |
---|
| 311 | [ ([[true;true;false;false;true]]) @@ r ] |
---|
| 312 | | DIRECT b1 ⇒ λ_. |
---|
| 313 | [ ([[true;true;false;false;false;true;false;true]]); b1] |
---|
| 314 | | INDIRECT i1 ⇒ λ_. |
---|
| 315 | [ ([[true;true;false;false;false;true;true;i1]]) ] |
---|
| 316 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 317 | | XCHD addr1 addr2 ⇒ |
---|
| 318 | match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with |
---|
| 319 | [ INDIRECT i1 ⇒ λ_. |
---|
| 320 | [ ([[true;true;false;true;false;true;true;i1]]) ] |
---|
| 321 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 322 | | XRL addrs ⇒ |
---|
| 323 | match addrs with |
---|
| 324 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 325 | match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with |
---|
[719] | 326 | [ REGISTER r ⇒ λ_. |
---|
| 327 | [ ([[false;true;true;false;true]]) @@ r ] |
---|
| 328 | | DIRECT b1 ⇒ λ_. |
---|
| 329 | [ ([[false;true;true;false;false;true;false;true]]); b1] |
---|
| 330 | | INDIRECT i1 ⇒ λ_. |
---|
| 331 | [ ([[false;true;true;false;false;true;true;i1]]) ] |
---|
| 332 | | DATA b1 ⇒ λ_. |
---|
| 333 | [ ([[false;true;true;false;false;true;false;false]]); b1] |
---|
| 334 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 335 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 336 | let b1 ≝ |
---|
| 337 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 338 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 339 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 340 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 341 | [ ACC_A ⇒ λ_. |
---|
| 342 | [ ([[false;true;true;false;false;false;true;false]]); b1 ] |
---|
| 343 | | DATA b2 ⇒ λ_. |
---|
| 344 | [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ] |
---|
[820] | 345 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 346 | ]. |
---|
[719] | 347 | |
---|
[820] | 348 | definition assembly1 ≝ |
---|
| 349 | λi: instruction. |
---|
| 350 | match i with |
---|
| 351 | [ ACALL addr ⇒ |
---|
| 352 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
| 353 | [ ADDR11 w ⇒ λ_. |
---|
| 354 | let 〈v1,v2〉 ≝ split ? 3 8 w in |
---|
| 355 | [ (v1 @@ [[true; false; false; false; true]]) ; v2 ] |
---|
| 356 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 357 | | AJMP addr ⇒ |
---|
| 358 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
| 359 | [ ADDR11 w ⇒ λ_. |
---|
| 360 | let 〈v1,v2〉 ≝ split ? 3 8 w in |
---|
| 361 | [ (v1 @@ [[false; false; false; false; true]]) ; v2 ] |
---|
| 362 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 363 | | JMP adptr ⇒ |
---|
| 364 | [ ([[false;true;true;true;false;false;true;true]]) ] |
---|
| 365 | | LCALL addr ⇒ |
---|
| 366 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
---|
| 367 | [ ADDR16 w ⇒ λ_. |
---|
| 368 | let 〈b1,b2〉 ≝ split ? 8 8 w in |
---|
| 369 | [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ] |
---|
| 370 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 371 | | LJMP 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 | [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ] |
---|
| 376 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 377 | | MOVC addr1 addr2 ⇒ |
---|
| 378 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with |
---|
| 379 | [ ACC_DPTR ⇒ λ_. |
---|
| 380 | [ ([[true;false;false;true;false;false;true;true]]) ] |
---|
| 381 | | ACC_PC ⇒ λ_. |
---|
| 382 | [ ([[true;false;false;false;false;false;true;true]]) ] |
---|
| 383 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 384 | | SJMP addr ⇒ |
---|
| 385 | match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with |
---|
| 386 | [ RELATIVE b1 ⇒ λ_. |
---|
| 387 | [ ([[true;false;false;false;false;false;false;false]]); b1 ] |
---|
| 388 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 389 | | RealInstruction instr ⇒ |
---|
| 390 | assembly_preinstruction [[ relative ]] |
---|
| 391 | (λx. |
---|
| 392 | match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with |
---|
| 393 | [ RELATIVE r ⇒ λ_. r |
---|
| 394 | | _ ⇒ λabsd. ⊥ |
---|
| 395 | ] (subaddressing_modein … x)) instr |
---|
| 396 | ]. |
---|
| 397 | cases absd |
---|
| 398 | qed. |
---|
| 399 | |
---|
[842] | 400 | definition is_relative_jump ≝ |
---|
| 401 | λi: preinstruction Identifier. |
---|
| 402 | match i with |
---|
| 403 | [ JC _ ⇒ True |
---|
| 404 | | JNC _ ⇒ True |
---|
| 405 | | JB _ _ ⇒ True |
---|
| 406 | | JNB _ _ ⇒ True |
---|
| 407 | | JBC _ _ ⇒ True |
---|
| 408 | | JZ _ ⇒ True |
---|
| 409 | | JNZ _ ⇒ True |
---|
| 410 | | CJNE _ _ ⇒ True |
---|
| 411 | | DJNZ _ _ ⇒ True |
---|
| 412 | | _ ⇒ False |
---|
| 413 | ]. |
---|
[719] | 414 | |
---|
[842] | 415 | definition pseudo_instruction_is_relative_jump: pseudo_instruction → Prop ≝ |
---|
| 416 | λi. |
---|
| 417 | match i with |
---|
| 418 | [ Instruction j ⇒ is_relative_jump j |
---|
| 419 | | _ ⇒ False ]. |
---|
| 420 | |
---|
[832] | 421 | inductive jump_length: Type[0] ≝ |
---|
| 422 | | short_jump: jump_length |
---|
| 423 | | medium_jump: jump_length |
---|
| 424 | | long_jump: jump_length. |
---|
| 425 | |
---|
[907] | 426 | definition jump_expansion_policy ≝ BitVectorTrie jump_length 16. |
---|
[832] | 427 | |
---|
[907] | 428 | definition find_right_call: Word → Word → nat × (option jump_length) ≝ |
---|
| 429 | (* medium call: 11 bits, only in "current segment" *) |
---|
| 430 | (* can this be done more efficiently with bit vector arithmetic? *) |
---|
| 431 | λpc.λaddress. |
---|
| 432 | let 〈p1, p2〉 ≝ split ? 5 11 address in |
---|
| 433 | let 〈a1, a2〉 ≝ split ? 5 11 pc in |
---|
| 434 | if eq_bv ? p1 a1 then (* we're in the same segment *) |
---|
| 435 | 〈2, Some ? medium_jump〉 |
---|
| 436 | else |
---|
| 437 | 〈3, Some ? long_jump〉. |
---|
| 438 | |
---|
| 439 | definition distance ≝ |
---|
| 440 | λx.λy.if gtb x y then x - y else y - x. |
---|
| 441 | |
---|
| 442 | definition find_right_jump: Word → Word → nat × (option jump_length) ≝ |
---|
| 443 | (* short jump: 8 bits signed *) |
---|
| 444 | (* medium jump: 11 bits, forward only *) |
---|
| 445 | λpc.λaddress. |
---|
| 446 | let pn ≝ nat_of_bitvector ? pc in |
---|
| 447 | let pa ≝ nat_of_bitvector ? address in |
---|
[914] | 448 | if ltb (distance pn pa) 127 then |
---|
[907] | 449 | 〈2, Some ? short_jump〉 |
---|
| 450 | else |
---|
| 451 | find_right_call pc address. |
---|
| 452 | |
---|
| 453 | definition find_right_relative_jump: Word → (BitVectorTrie Word 16) → |
---|
| 454 | Identifier → nat × (option jump_length) ≝ |
---|
| 455 | λpc.λlabels.λjmp. |
---|
| 456 | match lookup_opt ? ? jmp labels with |
---|
| 457 | [ None ⇒ 〈2, Some ? short_jump〉 |
---|
| 458 | | Some a ⇒ find_right_jump pc a ]. |
---|
| 459 | |
---|
| 460 | definition jep_relative: Word → (BitVectorTrie Word 16) → preinstruction Identifier → ? ≝ |
---|
| 461 | λpc.λlabels.λi. |
---|
| 462 | match i with |
---|
| 463 | [ JC jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 464 | | JNC jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 465 | | JB baddr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 466 | | JZ jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 467 | | JNZ jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 468 | | JBC baddr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 469 | | JNB baddr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 470 | | CJNE addr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 471 | | DJNZ addr jmp ⇒ find_right_relative_jump pc labels jmp |
---|
| 472 | | _ ⇒ 〈length ? (assembly_preinstruction ? (λx.zero ?) i), None …〉 ]. |
---|
| 473 | |
---|
| 474 | definition jump_expansion_policy_internal: pseudo_assembly_program → |
---|
| 475 | (BitVectorTrie Word 16) → jump_expansion_policy → |
---|
| 476 | ((BitVectorTrie Word 16) × jump_expansion_policy × bool)≝ |
---|
| 477 | λprogram.λlabels.λpolicy. |
---|
| 478 | let 〈pc, new_labels, new_policy, changed〉 ≝ |
---|
| 479 | foldl ? ? (λacc.λi:labelled_instruction. |
---|
| 480 | let 〈label, instr〉 ≝ i in |
---|
| 481 | let 〈pc,labels,policy,c0〉 ≝ acc in |
---|
| 482 | let 〈c1,new_labels〉 ≝ match label with |
---|
| 483 | [ None ⇒ 〈c0,labels〉 |
---|
| 484 | | Some l ⇒ |
---|
| 485 | match update ? ? pc l labels with |
---|
| 486 | [ None ⇒ 〈c0,labels〉 |
---|
| 487 | | Some x ⇒ 〈true, x〉 ] ] in |
---|
| 488 | let 〈pc_delta, jmp_len〉 ≝ match instr with |
---|
| 489 | [ Call c ⇒ |
---|
| 490 | match lookup_opt ? ? c new_labels with |
---|
| 491 | [ None ⇒ 〈2, Some ? medium_jump〉 |
---|
| 492 | | Some c_addr ⇒ find_right_call pc c_addr ] |
---|
| 493 | | Jmp j ⇒ |
---|
| 494 | match lookup_opt ? ? j new_labels with |
---|
| 495 | [ None ⇒ 〈2, Some ? short_jump〉 |
---|
| 496 | | Some j_addr ⇒ find_right_jump pc j_addr ] |
---|
| 497 | | Instruction i ⇒ jep_relative pc new_labels i |
---|
| 498 | | Mov _ _ ⇒ (* XXX is this correct? *) 〈2, None …〉 |
---|
| 499 | | Cost _ ⇒ 〈0, None …〉 |
---|
| 500 | | Comment _ ⇒ 〈0, None …〉 ] in |
---|
| 501 | let 〈new_pc,ignore〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in |
---|
| 502 | match jmp_len with |
---|
| 503 | [ None ⇒ 〈new_pc, new_labels, policy, c1〉 |
---|
| 504 | | Some j ⇒ |
---|
| 505 | match update ? ? pc j policy with |
---|
| 506 | [ None ⇒ 〈new_pc, new_labels, policy, c1〉 |
---|
| 507 | | Some x ⇒ 〈new_pc, new_labels, x, true〉 ] ] |
---|
| 508 | ) 〈zero ?, labels, policy, false〉 (\snd program) in |
---|
| 509 | 〈labels, policy, changed〉. |
---|
| 510 | |
---|
[842] | 511 | definition expand_relative_jump_internal: |
---|
| 512 | (Identifier → Word) → jump_length → Identifier → Word → ([[relative]] → preinstruction [[relative]]) → |
---|
| 513 | option (list instruction) |
---|
| 514 | ≝ |
---|
| 515 | λlookup_labels,jmp_len.λjmp:Identifier.λpc,i. |
---|
[832] | 516 | match jmp_len with |
---|
[842] | 517 | [ short_jump ⇒ |
---|
| 518 | let lookup_address ≝ lookup_labels jmp in |
---|
| 519 | let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in |
---|
| 520 | let 〈upper, lower〉 ≝ split ? 8 8 result in |
---|
| 521 | if eq_bv ? upper (zero 8) then |
---|
| 522 | let address ≝ RELATIVE lower in |
---|
| 523 | Some ? [ RealInstruction (i address) ] |
---|
| 524 | else |
---|
| 525 | None ? |
---|
| 526 | | medium_jump ⇒ None … |
---|
[832] | 527 | | long_jump ⇒ |
---|
[842] | 528 | Some ? |
---|
| 529 | [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2))); |
---|
[832] | 530 | SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *) |
---|
[842] | 531 | LJMP (ADDR16 (lookup_labels jmp)) |
---|
[832] | 532 | ] |
---|
| 533 | ]. |
---|
| 534 | @ I |
---|
| 535 | qed. |
---|
[825] | 536 | |
---|
[842] | 537 | definition expand_relative_jump: (Identifier → Word) → jump_length → Word → preinstruction Identifier → option (list instruction) ≝ |
---|
| 538 | λlookup_labels. |
---|
[832] | 539 | λjmp_len: jump_length. |
---|
[842] | 540 | λpc. |
---|
[832] | 541 | λi: preinstruction Identifier. |
---|
| 542 | let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in |
---|
| 543 | match i with |
---|
[842] | 544 | [ JC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JC ?) |
---|
| 545 | | JNC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNC ?) |
---|
| 546 | | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JB ? baddr) |
---|
| 547 | | JZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JZ ?) |
---|
| 548 | | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNZ ?) |
---|
| 549 | | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JBC ? baddr) |
---|
| 550 | | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNB ? baddr) |
---|
| 551 | | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (CJNE ? addr) |
---|
| 552 | | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (DJNZ ? addr) |
---|
[832] | 553 | | ADD arg1 arg2 ⇒ Some ? [ ADD ? arg1 arg2 ] |
---|
| 554 | | ADDC arg1 arg2 ⇒ Some ? [ ADDC ? arg1 arg2 ] |
---|
| 555 | | SUBB arg1 arg2 ⇒ Some ? [ SUBB ? arg1 arg2 ] |
---|
| 556 | | INC arg ⇒ Some ? [ INC ? arg ] |
---|
| 557 | | DEC arg ⇒ Some ? [ DEC ? arg ] |
---|
| 558 | | MUL arg1 arg2 ⇒ Some ? [ MUL ? arg1 arg2 ] |
---|
| 559 | | DIV arg1 arg2 ⇒ Some ? [ DIV ? arg1 arg2 ] |
---|
| 560 | | DA arg ⇒ Some ? [ DA ? arg ] |
---|
| 561 | | ANL arg ⇒ Some ? [ ANL ? arg ] |
---|
| 562 | | ORL arg ⇒ Some ? [ ORL ? arg ] |
---|
| 563 | | XRL arg ⇒ Some ? [ XRL ? arg ] |
---|
| 564 | | CLR arg ⇒ Some ? [ CLR ? arg ] |
---|
| 565 | | CPL arg ⇒ Some ? [ CPL ? arg ] |
---|
| 566 | | RL arg ⇒ Some ? [ RL ? arg ] |
---|
| 567 | | RR arg ⇒ Some ? [ RR ? arg ] |
---|
| 568 | | RLC arg ⇒ Some ? [ RLC ? arg ] |
---|
| 569 | | RRC arg ⇒ Some ? [ RRC ? arg ] |
---|
| 570 | | SWAP arg ⇒ Some ? [ SWAP ? arg ] |
---|
| 571 | | MOV arg ⇒ Some ? [ MOV ? arg ] |
---|
| 572 | | MOVX arg ⇒ Some ? [ MOVX ? arg ] |
---|
| 573 | | SETB arg ⇒ Some ? [ SETB ? arg ] |
---|
| 574 | | PUSH arg ⇒ Some ? [ PUSH ? arg ] |
---|
| 575 | | POP arg ⇒ Some ? [ POP ? arg ] |
---|
| 576 | | XCH arg1 arg2 ⇒ Some ? [ XCH ? arg1 arg2 ] |
---|
| 577 | | XCHD arg1 arg2 ⇒ Some ? [ XCHD ? arg1 arg2 ] |
---|
| 578 | | RET ⇒ Some ? [ RET ? ] |
---|
| 579 | | RETI ⇒ Some ? [ RETI ? ] |
---|
| 580 | | NOP ⇒ Some ? [ RealInstruction (NOP ?) ] |
---|
| 581 | ]. |
---|
| 582 | |
---|
| 583 | definition expand_pseudo_instruction: ? → ? → Word → jump_length → pseudo_instruction → option (list instruction) ≝ |
---|
[825] | 584 | λlookup_labels. |
---|
| 585 | λlookup_datalabels. |
---|
[832] | 586 | λpc. |
---|
| 587 | λjmp_len. |
---|
| 588 | λi. |
---|
| 589 | match i with |
---|
| 590 | [ Cost cost ⇒ Some ? [ ] |
---|
| 591 | | Comment comment ⇒ Some ? [ ] |
---|
| 592 | | Call call ⇒ |
---|
| 593 | match jmp_len with |
---|
| 594 | [ short_jump ⇒ None ? |
---|
| 595 | | medium_jump ⇒ |
---|
| 596 | let 〈ignore, address〉 ≝ split ? 5 11 (lookup_labels call) in |
---|
| 597 | let 〈fst_5, rest〉 ≝ split ? 5 11 pc in |
---|
| 598 | if eq_bv ? ignore fst_5 then |
---|
| 599 | let address ≝ ADDR11 address in |
---|
| 600 | Some ? ([ ACALL address ]) |
---|
| 601 | else |
---|
| 602 | None ? |
---|
| 603 | | long_jump ⇒ |
---|
| 604 | let address ≝ ADDR16 (lookup_labels call) in |
---|
| 605 | Some ? [ LCALL address ] |
---|
| 606 | ] |
---|
| 607 | | Mov d trgt ⇒ |
---|
| 608 | let address ≝ DATA16 (lookup_datalabels trgt) in |
---|
| 609 | Some ? [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))] |
---|
[842] | 610 | | Instruction instr ⇒ expand_relative_jump lookup_labels jmp_len pc instr |
---|
[832] | 611 | | Jmp jmp ⇒ |
---|
| 612 | match jmp_len with |
---|
| 613 | [ short_jump ⇒ |
---|
| 614 | let lookup_address ≝ lookup_labels jmp in |
---|
| 615 | let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in |
---|
| 616 | let 〈upper, lower〉 ≝ split ? 8 8 result in |
---|
| 617 | if eq_bv ? upper (zero 8) then |
---|
| 618 | let address ≝ RELATIVE lower in |
---|
| 619 | Some ? [ SJMP address ] |
---|
| 620 | else |
---|
| 621 | None ? |
---|
| 622 | | medium_jump ⇒ |
---|
| 623 | let address ≝ lookup_labels jmp in |
---|
| 624 | let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 address in |
---|
| 625 | let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in |
---|
| 626 | if eq_bv ? fst_5_addr fst_5_pc then |
---|
| 627 | let address ≝ ADDR11 rest_addr in |
---|
| 628 | Some ? ([ AJMP address ]) |
---|
| 629 | else |
---|
| 630 | None ? |
---|
| 631 | | long_jump ⇒ |
---|
| 632 | let address ≝ ADDR16 (lookup_labels jmp) in |
---|
| 633 | Some ? [ LJMP address ] |
---|
| 634 | ] |
---|
| 635 | ]. |
---|
[825] | 636 | @ I |
---|
| 637 | qed. |
---|
| 638 | |
---|
[914] | 639 | let rec jump_expansion_internal (n: nat) (program: pseudo_assembly_program) |
---|
| 640 | (old_labels: BitVectorTrie Word 16) (old_policy: BitVectorTrie jump_length 16) |
---|
| 641 | on n: jump_expansion_policy ≝ |
---|
| 642 | match n with |
---|
| 643 | [ O ⇒ old_policy |
---|
| 644 | | S n' ⇒ |
---|
| 645 | let 〈new_labels, new_policy, ch〉 ≝ |
---|
| 646 | jump_expansion_policy_internal program old_labels old_policy in |
---|
| 647 | jump_expansion_internal n' program new_labels new_policy ]. |
---|
| 648 | |
---|
| 649 | definition jump_expansion ≝ |
---|
| 650 | λpc.λprogram. |
---|
| 651 | let policy ≝ jump_expansion_internal (length ? (\snd program)) program (Stub ? ?) (Stub ? ?) in |
---|
| 652 | lookup ? ? pc policy long_jump. |
---|
| 653 | |
---|
[832] | 654 | definition assembly_1_pseudoinstruction ≝ |
---|
[913] | 655 | λprogram: pseudo_assembly_program. |
---|
| 656 | λpc: Word. |
---|
[832] | 657 | λlookup_labels. |
---|
| 658 | λlookup_datalabels. |
---|
| 659 | λi. |
---|
[914] | 660 | let expansion ≝ jump_expansion pc program in |
---|
[836] | 661 | match expand_pseudo_instruction lookup_labels lookup_datalabels pc expansion i with |
---|
[832] | 662 | [ None ⇒ None ? |
---|
[836] | 663 | | Some pseudos ⇒ |
---|
| 664 | let mapped ≝ map ? ? assembly1 pseudos in |
---|
| 665 | let flattened ≝ flatten ? mapped in |
---|
| 666 | let len ≝ length ? flattened in |
---|
| 667 | Some ? (〈len, flattened〉) |
---|
[832] | 668 | ]. |
---|
[913] | 669 | |
---|
[825] | 670 | definition construct_datalabels ≝ |
---|
| 671 | λpreamble. |
---|
| 672 | \fst (foldl ((BitVectorTrie Identifier 16) × nat) ? ( |
---|
| 673 | λt. λpreamble. |
---|
| 674 | let 〈datalabels, addr〉 ≝ t in |
---|
| 675 | let 〈name, size〉 ≝ preamble in |
---|
| 676 | let addr_16 ≝ bitvector_of_nat 16 addr in |
---|
| 677 | 〈insert ? ? name addr_16 datalabels, addr + size〉) |
---|
| 678 | 〈(Stub ? ?), 0〉 preamble). |
---|
| 679 | |
---|
[832] | 680 | definition construct_costs ≝ |
---|
| 681 | λprogram. |
---|
| 682 | λprogram_counter: nat. |
---|
| 683 | λlookup_labels. |
---|
| 684 | λlookup_datalabels. |
---|
| 685 | λcosts: BitVectorTrie Word 16. |
---|
| 686 | λi. |
---|
| 687 | match i with |
---|
[840] | 688 | [ Cost cost ⇒ |
---|
[832] | 689 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
| 690 | Some ? 〈program_counter, (insert ? ? program_counter_bv cost costs)〉 |
---|
| 691 | | _ ⇒ |
---|
| 692 | let pc_bv ≝ bitvector_of_nat ? program_counter in |
---|
[836] | 693 | let pc_delta_assembled ≝ |
---|
| 694 | assembly_1_pseudoinstruction program pc_bv |
---|
| 695 | lookup_labels lookup_datalabels i |
---|
| 696 | in |
---|
| 697 | match pc_delta_assembled with |
---|
[832] | 698 | [ None ⇒ None ? |
---|
[836] | 699 | | Some pc_delta_assembled ⇒ |
---|
| 700 | let 〈pc_delta, assembled〉 ≝ pc_delta_assembled in |
---|
[837] | 701 | Some ? 〈pc_delta + program_counter, costs〉 |
---|
[832] | 702 | ] |
---|
| 703 | ]. |
---|
| 704 | |
---|
| 705 | definition build_maps ≝ |
---|
| 706 | λinstr_list. |
---|
| 707 | let result ≝ foldl (option ((BitVectorTrie Word 16) × (nat × (BitVectorTrie Word 16)))) ? |
---|
| 708 | (λt. λi. |
---|
| 709 | let 〈label, i〉 ≝ i in |
---|
| 710 | match t with |
---|
| 711 | [ None ⇒ None ? |
---|
| 712 | | Some t ⇒ |
---|
| 713 | let 〈labels, pc_costs〉 ≝ t in |
---|
| 714 | let 〈program_counter, costs〉 ≝ pc_costs in |
---|
| 715 | let labels ≝ |
---|
| 716 | match label with |
---|
| 717 | [ None ⇒ labels |
---|
| 718 | | Some label ⇒ |
---|
| 719 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
| 720 | insert ? ? label program_counter_bv labels |
---|
| 721 | ] |
---|
| 722 | in |
---|
| 723 | match construct_costs instr_list program_counter (λx. zero ?) (λx. zero ?) costs i with |
---|
| 724 | [ None ⇒ None ? |
---|
| 725 | | Some construct ⇒ Some ? 〈labels, construct〉 |
---|
| 726 | ] |
---|
| 727 | ]) (Some ? 〈(Stub ? ?), 〈0, (Stub ? ?)〉〉) (\snd instr_list) in |
---|
| 728 | match result with |
---|
| 729 | [ None ⇒ None ? |
---|
| 730 | | Some result ⇒ |
---|
| 731 | let 〈labels, pc_costs〉 ≝ result in |
---|
| 732 | let 〈pc, costs〉 ≝ pc_costs in |
---|
| 733 | if gtb pc (2^16) then |
---|
| 734 | None ? |
---|
| 735 | else |
---|
| 736 | Some ? 〈labels, costs〉 |
---|
| 737 | ]. |
---|
| 738 | |
---|
[836] | 739 | definition assembly: pseudo_assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝ |
---|
[825] | 740 | λp. |
---|
| 741 | let 〈preamble, instr_list〉 ≝ p in |
---|
[832] | 742 | match build_maps p with |
---|
| 743 | [ None ⇒ None ? |
---|
| 744 | | Some labels_costs ⇒ |
---|
[844] | 745 | let datalabels ≝ construct_datalabels preamble in |
---|
[832] | 746 | let 〈labels,costs〉 ≝ labels_costs in |
---|
| 747 | let lookup_labels ≝ λx. lookup ? ? x labels (zero ?) in |
---|
| 748 | let lookup_datalabels ≝ λx. lookup ? ? x datalabels (zero ?) in |
---|
[836] | 749 | let result ≝ foldr ? ? (λx. λy. |
---|
[832] | 750 | match y with |
---|
| 751 | [ None ⇒ None ? |
---|
| 752 | | Some lst ⇒ |
---|
[836] | 753 | let 〈pc, y〉 ≝ lst in |
---|
| 754 | let x ≝ assembly_1_pseudoinstruction p pc lookup_labels lookup_datalabels (\snd x) in |
---|
[832] | 755 | match x with |
---|
| 756 | [ None ⇒ None ? |
---|
[836] | 757 | | Some x ⇒ |
---|
| 758 | let 〈pc_delta, program〉 ≝ x in |
---|
| 759 | let 〈new_pc, flags〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in |
---|
| 760 | Some ? 〈new_pc, (program @ y)〉 |
---|
[832] | 761 | ] |
---|
[836] | 762 | ]) (Some ? 〈(zero ?), [ ]〉) instr_list |
---|
| 763 | in |
---|
| 764 | match result with |
---|
| 765 | [ None ⇒ None ? |
---|
| 766 | | Some result ⇒ Some ? (〈\snd result, costs〉) |
---|
| 767 | ] |
---|
[844] | 768 | ]. |
---|
[832] | 769 | |
---|
[820] | 770 | definition assembly_unlabelled_program: assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝ |
---|
[846] | 771 | λp. Some ? (〈foldr ? ? (λi,l. assembly1 i @ l) [ ] p, Stub …〉). |
---|