[719] | 1 | include "ASM/ASM.ma". |
---|
| 2 | include "ASM/Arithmetic.ma". |
---|
| 3 | include "ASM/Fetch.ma". |
---|
| 4 | include "ASM/Status.ma". |
---|
[1459] | 5 | include alias "basics/logic.ma". |
---|
[1528] | 6 | include alias "arithmetics/nat.ma". |
---|
[1948] | 7 | include "utilities/extralib.ma". |
---|
[719] | 8 | |
---|
[989] | 9 | (**************************************** START OF POLICY ABSTRACTION ********************) |
---|
| 10 | |
---|
[1925] | 11 | (* definition of & operations on jump length *) |
---|
[1615] | 12 | inductive jump_length: Type[0] ≝ |
---|
| 13 | | short_jump: jump_length |
---|
[2101] | 14 | | absolute_jump: jump_length |
---|
[1925] | 15 | | long_jump: jump_length. |
---|
[2048] | 16 | |
---|
| 17 | (* Functions that define the conditions under which jumps are possible *) |
---|
| 18 | definition 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 |
---|
[2051] | 22 | let 〈upper, lower〉 ≝ vsplit ? 9 7 result in |
---|
[2048] | 23 | if get_index' ? 2 0 flags then |
---|
[2051] | 24 | 〈eq_bv 9 upper [[true;true;true;true;true;true;true;true;true]], true:::lower〉 |
---|
[2048] | 25 | else |
---|
[2051] | 26 | 〈eq_bv 9 upper (zero …), false:::lower〉. |
---|
[2048] | 27 | |
---|
[2101] | 28 | definition absolute_jump_cond: Word → Word → (*pseudo_instruction →*) |
---|
[2048] | 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〉. |
---|
[1615] | 34 | |
---|
| 35 | definition 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 | | MOV addrs ⇒ |
---|
| 186 | match addrs with |
---|
| 187 | [ inl 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 ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 196 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
| 197 | [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ] |
---|
| 198 | | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ] |
---|
| 199 | | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ] |
---|
| 200 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ] |
---|
| 201 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 202 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 203 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with |
---|
| 204 | [ REGISTER r ⇒ λ_. |
---|
| 205 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with |
---|
| 206 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ] |
---|
| 207 | | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ] |
---|
| 208 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ] |
---|
| 209 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 210 | | INDIRECT i1 ⇒ λ_. |
---|
| 211 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with |
---|
| 212 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ] |
---|
| 213 | | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ] |
---|
| 214 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ] |
---|
| 215 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 216 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 217 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 218 | let b1 ≝ |
---|
| 219 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 220 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 221 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 222 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with |
---|
| 223 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1] |
---|
| 224 | | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ] |
---|
| 225 | | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ] |
---|
| 226 | | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ] |
---|
| 227 | | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ] |
---|
| 228 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 229 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 230 | match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with |
---|
| 231 | [ DATA16 w ⇒ λ_. |
---|
[2032] | 232 | let b1_b2 ≝ vsplit ? 8 8 w in |
---|
[1615] | 233 | let b1 ≝ \fst b1_b2 in |
---|
| 234 | let b2 ≝ \snd b1_b2 in |
---|
| 235 | [ ([[true;false;false;true;false;false;false;false]]); b1; b2] |
---|
| 236 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 237 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 238 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 239 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 240 | [ ([[true;false;true;false;false;false;true;false]]); b1 ] |
---|
| 241 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 242 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 243 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 244 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 245 | [ ([[true;false;false;true;false;false;true;false]]); b1 ] |
---|
| 246 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 247 | | MOVX addrs ⇒ |
---|
| 248 | match addrs with |
---|
| 249 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 250 | match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
| 251 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
| 252 | [ ([[true;true;true;false;false;false;true;i1]]) ] |
---|
| 253 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
| 254 | [ ([[true;true;true;false;false;false;false;false]]) ] |
---|
| 255 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 256 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 257 | match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
| 258 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
| 259 | [ ([[true;true;true;true;false;false;true;i1]]) ] |
---|
| 260 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
| 261 | [ ([[true;true;true;true;false;false;false;false]]) ] |
---|
| 262 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 263 | | MUL addr1 addr2 ⇒ |
---|
| 264 | [ ([[true;false;true;false;false;true;false;false]]) ] |
---|
| 265 | | NOP ⇒ |
---|
| 266 | [ ([[false;false;false;false;false;false;false;false]]) ] |
---|
| 267 | | ORL addrs ⇒ |
---|
| 268 | match addrs with |
---|
| 269 | [ inl addrs ⇒ |
---|
| 270 | match addrs with |
---|
| 271 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 272 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with |
---|
| 273 | [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ] |
---|
| 274 | | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ] |
---|
| 275 | | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ] |
---|
| 276 | | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ] |
---|
| 277 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 278 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 279 | let b1 ≝ |
---|
| 280 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 281 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 282 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 283 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 284 | [ ACC_A ⇒ λ_. |
---|
| 285 | [ ([[false;true;false;false;false;false;true;false]]); b1 ] |
---|
| 286 | | DATA b2 ⇒ λ_. |
---|
| 287 | [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ] |
---|
| 288 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 289 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 290 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with |
---|
| 291 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 292 | [ ([[false;true;true;true;false;false;true;false]]); b1 ] |
---|
| 293 | | N_BIT_ADDR b1 ⇒ λ_. |
---|
| 294 | [ ([[true;false;true;false;false;false;false;false]]); b1 ] |
---|
| 295 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 296 | | POP addr ⇒ |
---|
| 297 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 298 | [ DIRECT b1 ⇒ λ_. |
---|
| 299 | [ ([[true;true;false;true;false;false;false;false]]) ; b1 ] |
---|
| 300 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 301 | | PUSH addr ⇒ |
---|
| 302 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 303 | [ DIRECT b1 ⇒ λ_. |
---|
| 304 | [ ([[true;true;false;false;false;false;false;false]]) ; b1 ] |
---|
| 305 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 306 | | RET ⇒ |
---|
| 307 | [ ([[false;false;true;false;false;false;true;false]]) ] |
---|
| 308 | | RETI ⇒ |
---|
| 309 | [ ([[false;false;true;true;false;false;true;false]]) ] |
---|
| 310 | | RL addr ⇒ |
---|
| 311 | [ ([[false;false;true;false;false;false;true;true]]) ] |
---|
| 312 | | RLC addr ⇒ |
---|
| 313 | [ ([[false;false;true;true;false;false;true;true]]) ] |
---|
| 314 | | RR addr ⇒ |
---|
| 315 | [ ([[false;false;false;false;false;false;true;true]]) ] |
---|
| 316 | | RRC addr ⇒ |
---|
| 317 | [ ([[false;false;false;true;false;false;true;true]]) ] |
---|
| 318 | | SETB addr ⇒ |
---|
| 319 | match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with |
---|
| 320 | [ CARRY ⇒ λ_. |
---|
| 321 | [ ([[true;true;false;true;false;false;true;true]]) ] |
---|
| 322 | | BIT_ADDR b1 ⇒ λ_. |
---|
| 323 | [ ([[true;true;false;true;false;false;true;false]]); b1 ] |
---|
| 324 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 325 | | SUBB addr1 addr2 ⇒ |
---|
| 326 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
| 327 | [ REGISTER r ⇒ λ_. |
---|
| 328 | [ ([[true;false;false;true;true]]) @@ r ] |
---|
| 329 | | DIRECT b1 ⇒ λ_. |
---|
| 330 | [ ([[true;false;false;true;false;true;false;true]]); b1] |
---|
| 331 | | INDIRECT i1 ⇒ λ_. |
---|
| 332 | [ ([[true;false;false;true;false;true;true;i1]]) ] |
---|
| 333 | | DATA b1 ⇒ λ_. |
---|
| 334 | [ ([[true;false;false;true;false;true;false;false]]); b1] |
---|
| 335 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 336 | | SWAP addr ⇒ |
---|
| 337 | [ ([[true;true;false;false;false;true;false;false]]) ] |
---|
| 338 | | XCH addr1 addr2 ⇒ |
---|
| 339 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with |
---|
| 340 | [ REGISTER r ⇒ λ_. |
---|
| 341 | [ ([[true;true;false;false;true]]) @@ r ] |
---|
| 342 | | DIRECT b1 ⇒ λ_. |
---|
| 343 | [ ([[true;true;false;false;false;true;false;true]]); b1] |
---|
| 344 | | INDIRECT i1 ⇒ λ_. |
---|
| 345 | [ ([[true;true;false;false;false;true;true;i1]]) ] |
---|
| 346 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 347 | | XCHD addr1 addr2 ⇒ |
---|
| 348 | match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with |
---|
| 349 | [ INDIRECT i1 ⇒ λ_. |
---|
| 350 | [ ([[true;true;false;true;false;true;true;i1]]) ] |
---|
| 351 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 352 | | XRL addrs ⇒ |
---|
| 353 | match addrs with |
---|
| 354 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 355 | match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with |
---|
| 356 | [ REGISTER r ⇒ λ_. |
---|
| 357 | [ ([[false;true;true;false;true]]) @@ r ] |
---|
| 358 | | DIRECT b1 ⇒ λ_. |
---|
| 359 | [ ([[false;true;true;false;false;true;false;true]]); b1] |
---|
| 360 | | INDIRECT i1 ⇒ λ_. |
---|
| 361 | [ ([[false;true;true;false;false;true;true;i1]]) ] |
---|
| 362 | | DATA b1 ⇒ λ_. |
---|
| 363 | [ ([[false;true;true;false;false;true;false;false]]); b1] |
---|
| 364 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 365 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 366 | let b1 ≝ |
---|
| 367 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 368 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 369 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 370 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 371 | [ ACC_A ⇒ λ_. |
---|
| 372 | [ ([[false;true;true;false;false;false;true;false]]); b1 ] |
---|
| 373 | | DATA b2 ⇒ λ_. |
---|
| 374 | [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ] |
---|
| 375 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 376 | ]. |
---|
| 377 | |
---|
| 378 | definition assembly1 ≝ |
---|
| 379 | λi: instruction. |
---|
| 380 | match i with |
---|
| 381 | [ ACALL addr ⇒ |
---|
| 382 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
| 383 | [ ADDR11 w ⇒ λ_. |
---|
[2032] | 384 | let v1_v2 ≝ vsplit ? 3 8 w in |
---|
[1615] | 385 | let v1 ≝ \fst v1_v2 in |
---|
| 386 | let v2 ≝ \snd v1_v2 in |
---|
| 387 | [ (v1 @@ [[true; false; false; false; true]]) ; v2 ] |
---|
| 388 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 389 | | AJMP addr ⇒ |
---|
| 390 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
| 391 | [ ADDR11 w ⇒ λ_. |
---|
[2032] | 392 | let v1_v2 ≝ vsplit ? 3 8 w in |
---|
[1615] | 393 | let v1 ≝ \fst v1_v2 in |
---|
| 394 | let v2 ≝ \snd v1_v2 in |
---|
| 395 | [ (v1 @@ [[false; false; false; false; true]]) ; v2 ] |
---|
| 396 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 397 | | JMP adptr ⇒ |
---|
| 398 | [ ([[false;true;true;true;false;false;true;true]]) ] |
---|
| 399 | | LCALL addr ⇒ |
---|
| 400 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
---|
| 401 | [ ADDR16 w ⇒ λ_. |
---|
[2032] | 402 | let b1_b2 ≝ vsplit ? 8 8 w in |
---|
[1615] | 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 ⇒ λ_. |
---|
[2032] | 410 | let b1_b2 ≝ vsplit ? 8 8 w in |
---|
[1615] | 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 |
---|
[1592] | 436 | qed. |
---|
| 437 | |
---|
[2015] | 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 |
---|
[2022] | 440 | the beginning. |
---|
[2015] | 441 | *) |
---|
[1905] | 442 | definition expand_relative_jump_internal: |
---|
[1948] | 443 | ∀lookup_labels:Identifier → Word.∀sigma:Word → Word. |
---|
[1905] | 444 | Identifier → Word → ([[relative]] → preinstruction [[relative]]) → |
---|
| 445 | list instruction |
---|
[1615] | 446 | ≝ |
---|
[1934] | 447 | λlookup_labels.λsigma.λlbl.λppc,i. |
---|
[1948] | 448 | let lookup_address ≝ sigma (lookup_labels lbl) in |
---|
[2022] | 449 | let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in |
---|
[2048] | 450 | let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in |
---|
| 451 | if sj_possible |
---|
| 452 | then |
---|
| 453 | let address ≝ RELATIVE disp in |
---|
[1905] | 454 | [ RealInstruction (i address) ] |
---|
| 455 | else |
---|
[1615] | 456 | [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2))); |
---|
| 457 | SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *) |
---|
[1649] | 458 | LJMP (ADDR16 lookup_address) |
---|
[1905] | 459 | ]. |
---|
[1948] | 460 | % |
---|
[1615] | 461 | qed. |
---|
| 462 | |
---|
[1905] | 463 | definition expand_relative_jump: |
---|
| 464 | ∀lookup_labels.∀sigma. |
---|
| 465 | Word → (*jump_length →*) |
---|
| 466 | preinstruction Identifier → list instruction ≝ |
---|
| 467 | λlookup_labels: Identifier → Word. |
---|
[1948] | 468 | λsigma:Word → Word. |
---|
[1934] | 469 | λppc: Word. |
---|
[1905] | 470 | (*λjmp_len: jump_length.*) |
---|
[1615] | 471 | λi: preinstruction Identifier. |
---|
[1934] | 472 | (*let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in*) |
---|
[1615] | 473 | match i with |
---|
[1934] | 474 | [ JC jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JC ?) |
---|
| 475 | | JNC jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JNC ?) |
---|
| 476 | | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JB ? baddr) |
---|
| 477 | | JZ jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JZ ?) |
---|
| 478 | | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JNZ ?) |
---|
| 479 | | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JBC ? baddr) |
---|
| 480 | | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (JNB ? baddr) |
---|
| 481 | | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (CJNE ? addr) |
---|
| 482 | | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels sigma jmp ppc (DJNZ ? addr) |
---|
[1905] | 483 | | ADD arg1 arg2 ⇒ [ ADD ? arg1 arg2 ] |
---|
| 484 | | ADDC arg1 arg2 ⇒ [ ADDC ? arg1 arg2 ] |
---|
| 485 | | SUBB arg1 arg2 ⇒ [ SUBB ? arg1 arg2 ] |
---|
| 486 | | INC arg ⇒ [ INC ? arg ] |
---|
| 487 | | DEC arg ⇒ [ DEC ? arg ] |
---|
| 488 | | MUL arg1 arg2 ⇒ [ MUL ? arg1 arg2 ] |
---|
| 489 | | DIV arg1 arg2 ⇒ [ DIV ? arg1 arg2 ] |
---|
| 490 | | DA arg ⇒ [ DA ? arg ] |
---|
| 491 | | ANL arg ⇒ [ ANL ? arg ] |
---|
| 492 | | ORL arg ⇒ [ ORL ? arg ] |
---|
| 493 | | XRL arg ⇒ [ XRL ? arg ] |
---|
| 494 | | CLR arg ⇒ [ CLR ? arg ] |
---|
| 495 | | CPL arg ⇒ [ CPL ? arg ] |
---|
| 496 | | RL arg ⇒ [ RL ? arg ] |
---|
| 497 | | RR arg ⇒ [ RR ? arg ] |
---|
| 498 | | RLC arg ⇒ [ RLC ? arg ] |
---|
| 499 | | RRC arg ⇒ [ RRC ? arg ] |
---|
| 500 | | SWAP arg ⇒ [ SWAP ? arg ] |
---|
| 501 | | MOV arg ⇒ [ MOV ? arg ] |
---|
| 502 | | MOVX arg ⇒ [ MOVX ? arg ] |
---|
| 503 | | SETB arg ⇒ [ SETB ? arg ] |
---|
| 504 | | PUSH arg ⇒ [ PUSH ? arg ] |
---|
| 505 | | POP arg ⇒ [ POP ? arg ] |
---|
| 506 | | XCH arg1 arg2 ⇒ [ XCH ? arg1 arg2 ] |
---|
| 507 | | XCHD arg1 arg2 ⇒ [ XCHD ? arg1 arg2 ] |
---|
| 508 | | RET ⇒ [ RET ? ] |
---|
| 509 | | RETI ⇒ [ RETI ? ] |
---|
| 510 | | NOP ⇒ [ RealInstruction (NOP ?) ] |
---|
[1615] | 511 | ]. |
---|
| 512 | |
---|
[1905] | 513 | definition expand_pseudo_instruction: |
---|
[1948] | 514 | ∀lookup_labels. |
---|
| 515 | ∀sigma: Word → Word. |
---|
| 516 | ∀policy: Word → bool. |
---|
| 517 | Word → ? → pseudo_instruction → list instruction ≝ |
---|
| 518 | λlookup_labels: Identifier → Word. |
---|
| 519 | λsigma: Word → Word. |
---|
| 520 | λpolicy: Word → bool. |
---|
[1934] | 521 | λppc. |
---|
[1905] | 522 | λlookup_datalabels:Identifier → Word. |
---|
[1615] | 523 | λi. |
---|
| 524 | match i with |
---|
[1905] | 525 | [ Cost cost ⇒ [ ] |
---|
| 526 | | Comment comment ⇒ [ ] |
---|
[1615] | 527 | | Call call ⇒ |
---|
[2022] | 528 | let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in |
---|
[2048] | 529 | let lookup_address ≝ sigma (lookup_labels call) in |
---|
[2101] | 530 | let 〈mj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in |
---|
[1948] | 531 | let do_a_long ≝ policy ppc in |
---|
[2048] | 532 | if mj_possible ∧ ¬ do_a_long then |
---|
| 533 | let address ≝ ADDR11 disp in |
---|
[1905] | 534 | [ ACALL address ] |
---|
| 535 | else |
---|
[2048] | 536 | let address ≝ ADDR16 lookup_address in |
---|
[1905] | 537 | [ LCALL address ] |
---|
[1615] | 538 | | Mov d trgt ⇒ |
---|
| 539 | let address ≝ DATA16 (lookup_datalabels trgt) in |
---|
[1905] | 540 | [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))] |
---|
[1934] | 541 | | Instruction instr ⇒ expand_relative_jump lookup_labels sigma ppc instr |
---|
[1615] | 542 | | Jmp jmp ⇒ |
---|
[2022] | 543 | let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in |
---|
[1948] | 544 | let do_a_long ≝ policy ppc in |
---|
| 545 | let lookup_address ≝ sigma (lookup_labels jmp) in |
---|
[2048] | 546 | let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in |
---|
| 547 | if sj_possible ∧ ¬ do_a_long then |
---|
| 548 | let address ≝ RELATIVE disp in |
---|
[1905] | 549 | [ SJMP address ] |
---|
| 550 | else |
---|
[2101] | 551 | let 〈mj_possible, disp2〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in |
---|
[2048] | 552 | if mj_possible ∧ ¬ do_a_long then |
---|
| 553 | let address ≝ ADDR11 disp2 in |
---|
[1905] | 554 | [ AJMP address ] |
---|
| 555 | else |
---|
[1934] | 556 | let address ≝ ADDR16 lookup_address in |
---|
[1905] | 557 | [ LJMP address ] |
---|
[1615] | 558 | ]. |
---|
[1948] | 559 | % |
---|
[1615] | 560 | qed. |
---|
[1649] | 561 | |
---|
| 562 | definition assembly_1_pseudoinstruction ≝ |
---|
| 563 | λlookup_labels. |
---|
[1948] | 564 | λsigma: Word → Word. |
---|
| 565 | λpolicy: Word → bool. |
---|
[1934] | 566 | λppc: Word. |
---|
[832] | 567 | λlookup_datalabels. |
---|
| 568 | λi. |
---|
[1948] | 569 | let pseudos ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels i in |
---|
[1649] | 570 | let mapped ≝ map ? ? assembly1 pseudos in |
---|
| 571 | let flattened ≝ flatten ? mapped in |
---|
| 572 | let pc_len ≝ length ? flattened in |
---|
| 573 | 〈pc_len, flattened〉. |
---|
[1667] | 574 | |
---|
[1934] | 575 | definition instruction_size ≝ |
---|
[1948] | 576 | λlookup_labels. |
---|
| 577 | λsigma: Word → Word. |
---|
| 578 | λpolicy: Word → bool. |
---|
| 579 | λppc. |
---|
| 580 | λi. |
---|
| 581 | \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc (λx.zero …) i). |
---|
[832] | 582 | |
---|
[2108] | 583 | |
---|
[1934] | 584 | (* label_map: identifier ↦ pseudo program counter *) |
---|
| 585 | definition label_map ≝ identifier_map ASMTag ℕ. |
---|
| 586 | |
---|
| 587 | (* Labels *) |
---|
| 588 | definition is_label ≝ |
---|
| 589 | λx:labelled_instruction.λl:Identifier. |
---|
| 590 | let 〈lbl,instr〉 ≝ x in |
---|
| 591 | match lbl with |
---|
| 592 | [ Some l' ⇒ l' = l |
---|
| 593 | | _ ⇒ False |
---|
| 594 | ]. |
---|
| 595 | |
---|
| 596 | lemma label_does_not_occur: |
---|
| 597 | ∀i:ℕ.∀p:list labelled_instruction.∀l:Identifier. |
---|
| 598 | is_label (nth i ? p 〈None ?, Comment [ ]〉) l → does_not_occur ?? l p = false. |
---|
| 599 | #i #p #l generalize in match i; elim p |
---|
| 600 | [ #i >nth_nil #H cases H |
---|
| 601 | | #h #t #IH #i cases i -i |
---|
| 602 | [ cases h #hi #hp cases hi |
---|
| 603 | [ normalize #H cases H |
---|
| 604 | | #l' #Heq whd in ⊢ (??%?); change with (eq_identifier ? l' l) in match (instruction_matches_identifier ????); |
---|
| 605 | whd in Heq; >Heq |
---|
| 606 | >eq_identifier_refl / by refl/ |
---|
| 607 | ] |
---|
| 608 | | #i #H whd in match (does_not_occur ????); |
---|
| 609 | whd in match (instruction_matches_identifier ????); |
---|
| 610 | cases h #hi #hp cases hi normalize nodelta |
---|
| 611 | [ @(IH i) @H |
---|
| 612 | | #l' @eq_identifier_elim |
---|
| 613 | [ normalize / by / |
---|
| 614 | | normalize #_ @(IH i) @H |
---|
| 615 | ] |
---|
| 616 | ] |
---|
| 617 | ] |
---|
| 618 | ] |
---|
[1556] | 619 | qed. |
---|
| 620 | |
---|
[1934] | 621 | (* The function that creates the label-to-address map *) |
---|
[1948] | 622 | definition create_label_cost_map0: ∀program:list labelled_instruction. |
---|
[1934] | 623 | (Σlabels_costs:label_map × (BitVectorTrie costlabel 16). (* Both on ppcs *) |
---|
| 624 | let 〈labels,costs〉 ≝ labels_costs in |
---|
| 625 | ∀l.occurs_exactly_once ?? l program → |
---|
[1943] | 626 | bitvector_of_nat ? (lookup_def ?? labels l 0) = |
---|
| 627 | address_of_word_labels_code_mem program l |
---|
[1934] | 628 | ) ≝ |
---|
| 629 | λprogram. |
---|
[2005] | 630 | \fst (pi1 ?? (foldl_strong (option Identifier × pseudo_instruction) |
---|
| 631 | (λprefix.Σlabels_costs_ppc:label_map × (BitVectorTrie costlabel 16) × ℕ. |
---|
[1934] | 632 | let 〈labels,costs,ppc〉 ≝ labels_costs_ppc in |
---|
| 633 | ppc = |prefix| ∧ |
---|
[1943] | 634 | ∀l.occurs_exactly_once ?? l prefix → |
---|
| 635 | bitvector_of_nat ? (lookup_def ?? labels l 0) = |
---|
[1948] | 636 | address_of_word_labels_code_mem prefix l) |
---|
[1934] | 637 | program |
---|
| 638 | (λprefix.λx.λtl.λprf.λlabels_costs_ppc. |
---|
[2005] | 639 | let 〈labels,costs,ppc〉 ≝ pi1 ?? labels_costs_ppc in |
---|
[1934] | 640 | let 〈label,instr〉 ≝ x in |
---|
| 641 | let labels ≝ |
---|
| 642 | match label with |
---|
| 643 | [ None ⇒ labels |
---|
| 644 | | Some l ⇒ add … labels l ppc |
---|
| 645 | ] in |
---|
| 646 | let costs ≝ |
---|
| 647 | match instr with |
---|
| 648 | [ Cost cost ⇒ insert … (bitvector_of_nat ? ppc) cost costs |
---|
| 649 | | _ ⇒ costs ] in |
---|
| 650 | 〈labels,costs,S ppc〉 |
---|
[2005] | 651 | ) 〈(empty_map …),(Stub ??),0〉)). |
---|
| 652 | [ normalize nodelta lapply (pi2 … labels_costs_ppc) >p >p1 normalize nodelta * #IH1 #IH2 |
---|
[1934] | 653 | -labels_costs_ppc % [>IH1 >length_append <plus_n_Sm <plus_n_O %] |
---|
| 654 | inversion label [#EQ | #l #EQ] |
---|
[1943] | 655 | [ #lbl #Hocc <address_of_word_labels_code_mem_None [2: @Hocc] normalize nodelta |
---|
| 656 | >occurs_exactly_once_None in Hocc; @(IH2 lbl) |
---|
| 657 | | #lbl normalize nodelta inversion (eq_identifier ? lbl l) |
---|
| 658 | [ #Heq #Hocc >(eq_identifier_eq … Heq) |
---|
| 659 | >address_of_word_labels_code_mem_Some_hit |
---|
| 660 | [ >IH1 >lookup_def_add_hit % |
---|
| 661 | | <(eq_identifier_eq … Heq) in Hocc; // |
---|
| 662 | ] |
---|
| 663 | | #Hneq #Hocc |
---|
| 664 | <address_of_word_labels_code_mem_Some_miss |
---|
| 665 | [ >lookup_def_add_miss |
---|
| 666 | [ @IH2 >occurs_exactly_once_Some_eq in Hocc; >eq_identifier_sym> Hneq // |
---|
| 667 | | % @neq_identifier_neq @Hneq |
---|
| 668 | ] |
---|
| 669 | | @Hocc |
---|
| 670 | | >eq_identifier_sym @Hneq |
---|
| 671 | ] |
---|
| 672 | ] |
---|
| 673 | ] |
---|
[1934] | 674 | | @pair_elim * #labels #costs #ppc #EQ destruct normalize nodelta % try % |
---|
[1943] | 675 | #l #abs cases (abs) |
---|
[1934] | 676 | | cases (foldl_strong ? (λ_.Σx.?) ???) * * #labels #costs #ppc normalize nodelta * |
---|
| 677 | #_ #H @H |
---|
| 678 | ] |
---|
| 679 | qed. |
---|
[1668] | 680 | |
---|
[1948] | 681 | (* The function that creates the label-to-address map *) |
---|
| 682 | definition create_label_cost_map: ∀program:list labelled_instruction. |
---|
| 683 | label_map × (BitVectorTrie costlabel 16) ≝ |
---|
| 684 | λprogram. |
---|
| 685 | pi1 … (create_label_cost_map0 program). |
---|
| 686 | |
---|
[1942] | 687 | theorem create_label_cost_map_ok: |
---|
| 688 | ∀pseudo_program: pseudo_assembly_program. |
---|
| 689 | let 〈labels, costs〉 ≝ create_label_cost_map (\snd pseudo_program) in |
---|
[1885] | 690 | ∀id. occurs_exactly_once ?? id (\snd pseudo_program) → |
---|
[1942] | 691 | bitvector_of_nat ? (lookup_def ?? labels id 0) = address_of_word_labels_code_mem (\snd pseudo_program) id. |
---|
[1948] | 692 | #p change with (pi1 … (create_label_cost_map0 ?)) in match (create_label_cost_map ?); @pi2 |
---|
[1942] | 693 | qed. |
---|
[2021] | 694 | |
---|
[2078] | 695 | definition sigma_policy_specification ≝ |
---|
| 696 | λprogram: pseudo_assembly_program. |
---|
| 697 | λsigma: Word → Word. |
---|
| 698 | λpolicy: Word → bool. |
---|
[2079] | 699 | sigma (zero …) = zero … ∧ |
---|
[2144] | 700 | let instr_list ≝ \snd program in |
---|
| 701 | ∀ppc: Word. ∀ppc_ok: nat_of_bitvector … ppc < |instr_list|. |
---|
[2078] | 702 | let pc ≝ sigma ppc in |
---|
| 703 | let labels ≝ \fst (create_label_cost_map instr_list) in |
---|
| 704 | let lookup_labels ≝ λx. bitvector_of_nat 16 (lookup_def … labels x 0) in |
---|
| 705 | let instruction ≝ \fst (fetch_pseudo_instruction instr_list ppc ppc_ok) in |
---|
| 706 | let next_pc ≝ sigma (add 16 ppc (bitvector_of_nat 16 1)) in |
---|
[2144] | 707 | next_pc = add 16 pc (bitvector_of_nat … (instruction_size lookup_labels sigma policy ppc instruction)) |
---|
| 708 | ∧ |
---|
| 709 | (nat_of_bitvector … pc < nat_of_bitvector … next_pc ∨ |
---|
[2146] | 710 | S (nat_of_bitvector … ppc) = |instr_list| ∧ next_pc = zero …). |
---|
[2078] | 711 | |
---|
[993] | 712 | definition assembly: |
---|
[1948] | 713 | ∀p: pseudo_assembly_program. |
---|
| 714 | ∀sigma: Word → Word. |
---|
| 715 | ∀policy: Word → bool. |
---|
[2021] | 716 | Σres:list Byte × (BitVectorTrie costlabel 16). |
---|
[2081] | 717 | sigma_policy_specification p sigma policy → |
---|
[2021] | 718 | let 〈preamble,instr_list〉 ≝ p in |
---|
[2110] | 719 | |instr_list| ≤ 2^16 → |
---|
[2021] | 720 | let 〈assembled,costs〉 ≝ res in |
---|
[2110] | 721 | |assembled| ≤ 2^16 ∧ |
---|
[2146] | 722 | (nat_of_bitvector … (sigma (bitvector_of_nat … (|instr_list|))) = |assembled| ∨ |
---|
| 723 | sigma (bitvector_of_nat … (|instr_list|)) = zero … ∧ |assembled| = 2^16) ∧ |
---|
[2021] | 724 | let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in |
---|
| 725 | let datalabels ≝ construct_datalabels preamble in |
---|
[2110] | 726 | let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in |
---|
[2021] | 727 | let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in |
---|
[2055] | 728 | ∀ppc. ∀ppc_ok:nat_of_bitvector … ppc < |instr_list|. |
---|
| 729 | let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc ppc_ok in |
---|
[2021] | 730 | let 〈len,assembledi〉 ≝ |
---|
| 731 | assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in |
---|
[2147] | 732 | |assembledi| ≤ |assembled| ∧ |
---|
[2021] | 733 | ∀j:nat. ∀H: j < |assembledi|. ∀K. |
---|
| 734 | nth_safe ? j assembledi H = |
---|
| 735 | nth_safe ? (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat ? j))) |
---|
| 736 | assembled K |
---|
[2146] | 737 | ≝ ?. cases daemon(* |
---|
[2081] | 738 | λp. |
---|
[1948] | 739 | λsigma. |
---|
| 740 | λpolicy. |
---|
[2021] | 741 | deplet 〈preamble, instr_list〉 as p_refl ≝ p in |
---|
[1948] | 742 | let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in |
---|
| 743 | let datalabels ≝ construct_datalabels preamble in |
---|
[2110] | 744 | let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in |
---|
[1948] | 745 | let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in |
---|
[2138] | 746 | let 〈next_pc,revcode〉 ≝ pi1 … ( |
---|
[2021] | 747 | foldl_strong |
---|
[993] | 748 | (option Identifier × pseudo_instruction) |
---|
[2021] | 749 | (λpre. Σppc_code:(Word × (list Byte)). |
---|
[2081] | 750 | sigma_policy_specification 〈preamble,instr_list〉 sigma policy → |
---|
[2110] | 751 | |instr_list| ≤ 2^16 → |
---|
[993] | 752 | let 〈ppc,code〉 ≝ ppc_code in |
---|
[2021] | 753 | ppc = bitvector_of_nat … (|pre|) ∧ |
---|
[2146] | 754 | (nat_of_bitvector … (sigma ppc) = |code| ∨ |
---|
| 755 | sigma ppc = zero … ∧ |code| = 2^16) ∧ |
---|
[2055] | 756 | ∀ppc'.∀ppc_ok'. |
---|
[2110] | 757 | (nat_of_bitvector … ppc' < nat_of_bitvector … ppc ∨ |pre| = 2^16) → |
---|
[2055] | 758 | let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' ppc_ok' in |
---|
[2021] | 759 | let 〈len,assembledi〉 ≝ |
---|
| 760 | assembly_1_pseudoinstruction lookup_labels sigma policy ppc' lookup_datalabels pi in |
---|
| 761 | ∀j:nat. ∀H: j < |assembledi|. ∀K. |
---|
| 762 | nth_safe ? j assembledi H = |
---|
| 763 | nth_safe ? (nat_of_bitvector … (add … (sigma ppc') (bitvector_of_nat ? j))) (reverse … code) K) |
---|
[993] | 764 | instr_list |
---|
[2021] | 765 | (λprefix,hd,tl,prf,ppc_code. |
---|
| 766 | let 〈ppc, code〉 ≝ pi1 … ppc_code in |
---|
[1948] | 767 | let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels (\snd hd) in |
---|
[1946] | 768 | let new_ppc ≝ add ? ppc (bitvector_of_nat ? 1) in |
---|
[2021] | 769 | 〈new_ppc, (reverse … program @ code)〉) |
---|
| 770 | 〈(zero ?), [ ]〉) |
---|
[985] | 771 | in |
---|
[2021] | 772 | 〈reverse … revcode, |
---|
[1948] | 773 | fold … (λppc.λcost.λpc_to_costs. insert … (sigma ppc) cost pc_to_costs) ppc_to_costs (Stub ??)〉. |
---|
[2021] | 774 | [ cases (foldl_strong ? (λx.Σy.?) ???) in p2; #ignore_revcode #Hfold #EQignore_revcode |
---|
[2081] | 775 | >EQignore_revcode in Hfold; #Hfold #sigma_pol_ok #instr_list_ok |
---|
[2110] | 776 | cases (Hfold sigma_pol_ok instr_list_ok) -Hfold * #Hfold1 #Hfold3 #Hfold2 whd >p1 whd % |
---|
| 777 | [2: #ppc #LTppc @Hfold2 >Hfold1 @(eqb_elim (|instr_list|) 2^16) |
---|
| 778 | [ #limit %2 @limit |
---|
| 779 | | #nlimit %1 >nat_of_bitvector_bitvector_of_nat_inverse try assumption |
---|
| 780 | @not_eq_to_le_to_lt assumption ] |
---|
[2138] | 781 | | >length_reverse <Hfold3 % |
---|
| 782 | [ @(transitive_le … (S (nat_of_bitvector … (sigma next_pc)))) |
---|
| 783 | [ // | change with (? < ?); @lt_nat_of_bitvector ] |
---|
| 784 | | >Hfold1 % ]] |
---|
[2081] | 785 | | * #sigma_pol_ok1 #_ #instr_list_ok % |
---|
| 786 | [ % // >sigma_pol_ok1 % ] |
---|
[2110] | 787 | #ppc' #ppc_ok' #abs @⊥ cases abs |
---|
| 788 | [#abs2 cases (not_le_Sn_O ?) [#H @(H abs2) | skip] |
---|
| 789 | |#abs2 change with (0 = S ?) in abs2; destruct(abs2) ] |
---|
[2081] | 790 | | * #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 |
---|
| 791 | #IH cases (IH ? instr_list_ok) [2: % assumption ] * #IH1 #IH3 #IH2 whd % [% |
---|
[2072] | 792 | [ >length_append normalize nodelta >IH1 (*CSC: TRUE, LEMMA NEEDED *) cases daemon |
---|
| 793 | |2: (*CSC: NEES JAAP INVARIANT*) cases daemon]] |
---|
[2110] | 794 | #ppc' #LTppc' cases hd in prf p2; #label #pi #prf #p2 * |
---|
| 795 | cases daemon (* |
---|
| 796 | ?????????????? PRIMA NON C'ERA L'OR :-( |
---|
| 797 | MANTENERE LO SCHEMA DI PRIMA CON DUE CASI, MA IN UN CASO ppc=0 OVERFLOWED |
---|
| 798 | [2: #eq_S_prefix_bound -IH @(IH2 ? LTppc') |
---|
| 799 | @pair_elim #pi' #newppc' #eq_fetch_pseudo_instruction' |
---|
| 800 | @pair_elim |
---|
| 801 | | #LTppc_ppc |
---|
| 802 | cases (le_to_or_lt_eq … LTppc_ppc) |
---|
| 803 | [2: #S_S_eq normalize nodelta in S_S_eq; |
---|
| 804 | (*CSC: TRUE, NEEDS SOME WORK *) |
---|
| 805 | cut (ppc' = ppc) [ cases daemon] -S_S_eq #EQppc' >EQppc' in LTppc'; -ppc' >prf |
---|
| 806 | >IH1 (* in ⊢ match % with [_ ⇒ ?];*) #LTppc lapply LTppc |
---|
| 807 | >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → match % with [_ ⇒ ?]); |
---|
| 808 | >fetch_pseudo_instruction_append |
---|
| 809 | [3: @le_S_S @le_O_n |
---|
| 810 | |2: lapply LTppc; >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → ?); #H @H |
---|
| 811 | |4: <prf <p_refl in instr_list_ok; #H @H ] |
---|
| 812 | #LTppc' @pair_elim #pi' #newppc' #EQpair destruct(EQpair) <IH1 >p2 |
---|
| 813 | #j #LTj >nat_of_bitvector_add |
---|
| 814 | >nat_of_bitvector_bitvector_of_nat_inverse |
---|
| 815 | [2,4: (* CSC: TRUE, NEEDS LEMMA, MAYBE ALREADY PROVED *) cases daemon |
---|
| 816 | |3: (*CSC: TRUE, NEEDS LEMMA *) cases daemon ] |
---|
| 817 | >reverse_append >reverse_reverse >IH3 <(length_reverse … code) |
---|
| 818 | @nth_safe_prepend |
---|
| 819 | | #LTppc'' |
---|
| 820 | cut (nat_of_bitvector … ppc' < |instr_list|) |
---|
| 821 | [ normalize nodelta in LTppc''; |
---|
| 822 | @(transitive_le … (nat_of_bitvector … ppc)) |
---|
| 823 | [2: >IH1 >prf >length_append >nat_of_bitvector_bitvector_of_nat_inverse |
---|
| 824 | [ // |
---|
| 825 | | <p_refl in instr_list_ok; #instr_list_ok |
---|
| 826 | @(transitive_le … (S (|instr_list|))) try assumption >prf >length_append // ] |
---|
| 827 | | @le_S_S_to_le >nat_of_bitvector_add in LTppc''; |
---|
| 828 | [ >commutative_plus #H @H |
---|
| 829 | | >nat_of_bitvector_bitvector_of_nat_inverse [2: // ] >commutative_plus |
---|
| 830 | @(transitive_le … (|instr_list|)) |
---|
| 831 | [2: <p_refl in instr_list_ok; #instr_list_ok assumption |
---|
| 832 | | >IH1 >prf >length_append @le_S_S >(commutative_plus (|prefix|)) |
---|
| 833 | >length_append >nat_of_bitvector_bitvector_of_nat_inverse |
---|
| 834 | [2: <p_refl in instr_list_ok; >prf >length_append #H |
---|
| 835 | @(transitive_le … H) // |
---|
| 836 | | @le_S_S // ]]]]] |
---|
[2071] | 837 | #X lapply (IH2 ppc' X) |
---|
[2021] | 838 | @pair_elim #pi' #newppc' #eq_fetch_pseudoinstruction |
---|
| 839 | @pair_elim #len' #assembledi' #eq_assembly_1_pseudoinstruction #IH |
---|
| 840 | change with (let 〈len,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi' in ∀j:ℕ. ∀H:j<|assembledi|.?) |
---|
| 841 | >eq_assembly_1_pseudoinstruction #j #LTj >reverse_append >reverse_reverse #K |
---|
| 842 | >IH |
---|
[2073] | 843 | [2: >length_reverse <IH3 (*CSC: NEEDS JAAP INVARIANT PLUS SOME WORK *) cases daemon |
---|
[2055] | 844 | | @shift_nth_prefix |
---|
[2071] | 845 | |3: @le_S_S_to_le @(transitive_le … LTppc'') >nat_of_bitvector_add |
---|
| 846 | [ >commutative_plus % |
---|
| 847 | | >commutative_plus >IH1 whd in ⊢ (?%?); |
---|
| 848 | @(transitive_le … (S (|instr_list|))) |
---|
| 849 | [2: <p_refl in instr_list_ok; #instr_list_ok assumption |
---|
| 850 | | >prf >length_append >length_append <plus_n_Sm >nat_of_bitvector_bitvector_of_nat_inverse |
---|
| 851 | [ // | <p_refl in instr_list_ok; >prf >length_append #H @(transitive_le … H) // ]]]] |
---|
[2146] | 852 | *)*)qed. |
---|
[832] | 853 | |
---|
[1948] | 854 | definition assembly_unlabelled_program: |
---|
| 855 | assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝ |
---|
| 856 | λp. |
---|
[2146] | 857 | Some … (〈foldr … (λi,l. assembly1 i @ l) [ ] p, Stub …〉). |
---|