| 1 | include "ASM/ASM.ma". |
|---|
| 2 | include "ASM/Arithmetic.ma". |
|---|
| 3 | include "ASM/Fetch.ma". |
|---|
| 4 | include "ASM/Status.ma". |
|---|
| 5 | include alias "basics/logic.ma". |
|---|
| 6 | include alias "arithmetics/nat.ma". |
|---|
| 7 | include "utilities/extralib.ma". |
|---|
| 8 | |
|---|
| 9 | (**************************************** START OF POLICY ABSTRACTION ********************) |
|---|
| 10 | |
|---|
| 11 | (* definition of & operations on jump length *) |
|---|
| 12 | inductive jump_length: Type[0] ≝ |
|---|
| 13 | | short_jump: jump_length |
|---|
| 14 | | absolute_jump: jump_length |
|---|
| 15 | | long_jump: jump_length. |
|---|
| 16 | |
|---|
| 17 | (* Functions that define the conditions under which jumps are possible *) |
|---|
| 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 |
|---|
| 22 | let 〈upper, lower〉 ≝ vsplit ? 9 7 result in |
|---|
| 23 | if get_index' ? 2 0 flags then |
|---|
| 24 | 〈eq_bv 9 upper [[true;true;true;true;true;true;true;true;true]], true:::lower〉 |
|---|
| 25 | else |
|---|
| 26 | 〈eq_bv 9 upper (zero …), false:::lower〉. |
|---|
| 27 | |
|---|
| 28 | definition absolute_jump_cond: Word → Word → (*pseudo_instruction →*) |
|---|
| 29 | bool × (BitVector 11) ≝ |
|---|
| 30 | λpc_plus_jmp_length.λaddr.(*λinstr.*) |
|---|
| 31 | let 〈fst_5_addr, rest_addr〉 ≝ vsplit bool 5 11 addr in |
|---|
| 32 | let 〈fst_5_pc, rest_pc〉 ≝ vsplit bool 5 11 pc_plus_jmp_length in |
|---|
| 33 | 〈eq_bv 5 fst_5_addr fst_5_pc, rest_addr〉. |
|---|
| 34 | |
|---|
| 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 ⇒ λ_. |
|---|
| 232 | let b1_b2 ≝ vsplit ? 8 8 w in |
|---|
| 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 ⇒ λ_. |
|---|
| 384 | let v1_v2 ≝ vsplit ? 3 8 w in |
|---|
| 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 ⇒ λ_. |
|---|
| 392 | let v1_v2 ≝ vsplit ? 3 8 w in |
|---|
| 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 ⇒ λ_. |
|---|
| 402 | let b1_b2 ≝ vsplit ? 8 8 w in |
|---|
| 403 | let b1 ≝ \fst b1_b2 in |
|---|
| 404 | let b2 ≝ \snd b1_b2 in |
|---|
| 405 | [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ] |
|---|
| 406 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
|---|
| 407 | | LJMP addr ⇒ |
|---|
| 408 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
|---|
| 409 | [ ADDR16 w ⇒ λ_. |
|---|
| 410 | let b1_b2 ≝ vsplit ? 8 8 w in |
|---|
| 411 | let b1 ≝ \fst b1_b2 in |
|---|
| 412 | let b2 ≝ \snd b1_b2 in |
|---|
| 413 | [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ] |
|---|
| 414 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
|---|
| 415 | | MOVC addr1 addr2 ⇒ |
|---|
| 416 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with |
|---|
| 417 | [ ACC_DPTR ⇒ λ_. |
|---|
| 418 | [ ([[true;false;false;true;false;false;true;true]]) ] |
|---|
| 419 | | ACC_PC ⇒ λ_. |
|---|
| 420 | [ ([[true;false;false;false;false;false;true;true]]) ] |
|---|
| 421 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
|---|
| 422 | | SJMP addr ⇒ |
|---|
| 423 | match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with |
|---|
| 424 | [ RELATIVE b1 ⇒ λ_. |
|---|
| 425 | [ ([[true;false;false;false;false;false;false;false]]); b1 ] |
|---|
| 426 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
|---|
| 427 | | RealInstruction instr ⇒ |
|---|
| 428 | assembly_preinstruction [[ relative ]] |
|---|
| 429 | (λx. |
|---|
| 430 | match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with |
|---|
| 431 | [ RELATIVE r ⇒ λ_. r |
|---|
| 432 | | _ ⇒ λabsd. ⊥ |
|---|
| 433 | ] (subaddressing_modein … x)) instr |
|---|
| 434 | ]. |
|---|
| 435 | cases absd |
|---|
| 436 | qed. |
|---|
| 437 | |
|---|
| 438 | (* XXX: pc_plus_sjmp_length used to be just sigma of ppc. This is incorrect |
|---|
| 439 | as relative lengths are computed from the *end* of the SJMP, not from |
|---|
| 440 | the beginning. |
|---|
| 441 | *) |
|---|
| 442 | definition expand_relative_jump_internal: |
|---|
| 443 | ∀lookup_labels:Identifier → Word.∀sigma:Word → Word. |
|---|
| 444 | Identifier → Word → ([[relative]] → preinstruction [[relative]]) → |
|---|
| 445 | list instruction |
|---|
| 446 | ≝ |
|---|
| 447 | λlookup_labels.λsigma.λlbl.λppc,i. |
|---|
| 448 | let lookup_address ≝ sigma (lookup_labels lbl) in |
|---|
| 449 | let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in |
|---|
| 450 | let 〈sj_possible, disp〉 ≝ short_jump_cond pc_plus_jmp_length lookup_address in |
|---|
| 451 | if sj_possible |
|---|
| 452 | then |
|---|
| 453 | let address ≝ RELATIVE disp in |
|---|
| 454 | [ RealInstruction (i address) ] |
|---|
| 455 | else |
|---|
| 456 | [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2))); |
|---|
| 457 | SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *) |
|---|
| 458 | LJMP (ADDR16 lookup_address) |
|---|
| 459 | ]. |
|---|
| 460 | % |
|---|
| 461 | qed. |
|---|
| 462 | |
|---|
| 463 | definition expand_relative_jump: |
|---|
| 464 | ∀lookup_labels.∀sigma. |
|---|
| 465 | Word → (*jump_length →*) |
|---|
| 466 | preinstruction Identifier → list instruction ≝ |
|---|
| 467 | λlookup_labels: Identifier → Word. |
|---|
| 468 | λsigma:Word → Word. |
|---|
| 469 | λppc: Word. |
|---|
| 470 | (*λjmp_len: jump_length.*) |
|---|
| 471 | λi: preinstruction Identifier. |
|---|
| 472 | (*let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in*) |
|---|
| 473 | match i with |
|---|
| 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) |
|---|
| 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 ?) ] |
|---|
| 511 | ]. |
|---|
| 512 | |
|---|
| 513 | definition expand_pseudo_instruction: |
|---|
| 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. |
|---|
| 521 | λppc. |
|---|
| 522 | λlookup_datalabels:Identifier → Word. |
|---|
| 523 | λi. |
|---|
| 524 | match i with |
|---|
| 525 | [ Cost cost ⇒ [ ] |
|---|
| 526 | | Comment comment ⇒ [ ] |
|---|
| 527 | | Call call ⇒ |
|---|
| 528 | let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in |
|---|
| 529 | let lookup_address ≝ sigma (lookup_labels call) in |
|---|
| 530 | let 〈mj_possible, disp〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in |
|---|
| 531 | let do_a_long ≝ policy ppc in |
|---|
| 532 | if mj_possible ∧ ¬ do_a_long then |
|---|
| 533 | let address ≝ ADDR11 disp in |
|---|
| 534 | [ ACALL address ] |
|---|
| 535 | else |
|---|
| 536 | let address ≝ ADDR16 lookup_address in |
|---|
| 537 | [ LCALL address ] |
|---|
| 538 | | Mov d trgt ⇒ |
|---|
| 539 | let address ≝ DATA16 (lookup_datalabels trgt) in |
|---|
| 540 | [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))] |
|---|
| 541 | | Instruction instr ⇒ expand_relative_jump lookup_labels sigma ppc instr |
|---|
| 542 | | Jmp jmp ⇒ |
|---|
| 543 | let pc_plus_jmp_length ≝ sigma (add … ppc (bitvector_of_nat … 1)) in |
|---|
| 544 | let do_a_long ≝ policy ppc in |
|---|
| 545 | let lookup_address ≝ sigma (lookup_labels jmp) in |
|---|
| 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 |
|---|
| 549 | [ SJMP address ] |
|---|
| 550 | else |
|---|
| 551 | let 〈mj_possible, disp2〉 ≝ absolute_jump_cond pc_plus_jmp_length lookup_address in |
|---|
| 552 | if mj_possible ∧ ¬ do_a_long then |
|---|
| 553 | let address ≝ ADDR11 disp2 in |
|---|
| 554 | [ AJMP address ] |
|---|
| 555 | else |
|---|
| 556 | let address ≝ ADDR16 lookup_address in |
|---|
| 557 | [ LJMP address ] |
|---|
| 558 | ]. |
|---|
| 559 | % |
|---|
| 560 | qed. |
|---|
| 561 | |
|---|
| 562 | definition assembly_1_pseudoinstruction ≝ |
|---|
| 563 | λlookup_labels. |
|---|
| 564 | λsigma: Word → Word. |
|---|
| 565 | λpolicy: Word → bool. |
|---|
| 566 | λppc: Word. |
|---|
| 567 | λlookup_datalabels. |
|---|
| 568 | λi. |
|---|
| 569 | let pseudos ≝ expand_pseudo_instruction lookup_labels sigma policy ppc lookup_datalabels i in |
|---|
| 570 | let mapped ≝ map ? ? assembly1 pseudos in |
|---|
| 571 | let flattened ≝ flatten ? mapped in |
|---|
| 572 | let pc_len ≝ length ? flattened in |
|---|
| 573 | 〈pc_len, flattened〉. |
|---|
| 574 | |
|---|
| 575 | definition instruction_size ≝ |
|---|
| 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). |
|---|
| 582 | |
|---|
| 583 | |
|---|
| 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 | ] |
|---|
| 619 | qed. |
|---|
| 620 | |
|---|
| 621 | (* The function that creates the label-to-address map *) |
|---|
| 622 | definition create_label_cost_map0: ∀program:list labelled_instruction. |
|---|
| 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 → |
|---|
| 626 | bitvector_of_nat ? (lookup_def ?? labels l 0) = |
|---|
| 627 | address_of_word_labels_code_mem program l |
|---|
| 628 | ) ≝ |
|---|
| 629 | λprogram. |
|---|
| 630 | \fst (pi1 ?? (foldl_strong (option Identifier × pseudo_instruction) |
|---|
| 631 | (λprefix.Σlabels_costs_ppc:label_map × (BitVectorTrie costlabel 16) × ℕ. |
|---|
| 632 | let 〈labels,costs,ppc〉 ≝ labels_costs_ppc in |
|---|
| 633 | ppc = |prefix| ∧ |
|---|
| 634 | ∀l.occurs_exactly_once ?? l prefix → |
|---|
| 635 | bitvector_of_nat ? (lookup_def ?? labels l 0) = |
|---|
| 636 | address_of_word_labels_code_mem prefix l) |
|---|
| 637 | program |
|---|
| 638 | (λprefix.λx.λtl.λprf.λlabels_costs_ppc. |
|---|
| 639 | let 〈labels,costs,ppc〉 ≝ pi1 ?? labels_costs_ppc in |
|---|
| 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〉 |
|---|
| 651 | ) 〈(empty_map …),(Stub ??),0〉)). |
|---|
| 652 | [ normalize nodelta lapply (pi2 … labels_costs_ppc) >p >p1 normalize nodelta * #IH1 #IH2 |
|---|
| 653 | -labels_costs_ppc % [>IH1 >length_append <plus_n_Sm <plus_n_O %] |
|---|
| 654 | inversion label [#EQ | #l #EQ] |
|---|
| 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 | ] |
|---|
| 674 | | @pair_elim * #labels #costs #ppc #EQ destruct normalize nodelta % try % |
|---|
| 675 | #l #abs cases (abs) |
|---|
| 676 | | cases (foldl_strong ? (λ_.Σx.?) ???) * * #labels #costs #ppc normalize nodelta * |
|---|
| 677 | #_ #H @H |
|---|
| 678 | ] |
|---|
| 679 | qed. |
|---|
| 680 | |
|---|
| 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 | |
|---|
| 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 |
|---|
| 690 | ∀id. occurs_exactly_once ?? id (\snd pseudo_program) → |
|---|
| 691 | bitvector_of_nat ? (lookup_def ?? labels id 0) = address_of_word_labels_code_mem (\snd pseudo_program) id. |
|---|
| 692 | #p change with (pi1 … (create_label_cost_map0 ?)) in match (create_label_cost_map ?); @pi2 |
|---|
| 693 | qed. |
|---|
| 694 | |
|---|
| 695 | definition sigma_policy_specification ≝ |
|---|
| 696 | λprogram: pseudo_assembly_program. |
|---|
| 697 | λsigma: Word → Word. |
|---|
| 698 | λpolicy: Word → bool. |
|---|
| 699 | sigma (zero …) = zero … ∧ |
|---|
| 700 | let instr_list ≝ \snd program in |
|---|
| 701 | ∀ppc: Word. ∀ppc_ok: nat_of_bitvector … ppc < |instr_list|. |
|---|
| 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 |
|---|
| 707 | next_pc = add 16 pc (bitvector_of_nat … (instruction_size lookup_labels sigma policy ppc instruction)) |
|---|
| 708 | ∧ |
|---|
| 709 | (nat_of_bitvector … pc + instruction_size lookup_labels sigma policy ppc instruction < 2^16 |
|---|
| 710 | ∨ |
|---|
| 711 | (∀ppc'. ∀ppc_ok':nat_of_bitvector … ppc' < |instr_list|. nat_of_bitvector … ppc < nat_of_bitvector … ppc' → |
|---|
| 712 | let instruction' ≝ \fst (fetch_pseudo_instruction instr_list ppc' ppc_ok') in |
|---|
| 713 | instruction_size lookup_labels sigma policy ppc' instruction' = 0) |
|---|
| 714 | ∧ |
|---|
| 715 | nat_of_bitvector … pc + instruction_size lookup_labels sigma policy ppc instruction = 2^16). |
|---|
| 716 | |
|---|
| 717 | |
|---|
| 718 | lemma fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels: |
|---|
| 719 | ∀lookup_labels,sigma,policy,ppc,pi. |
|---|
| 720 | ∀lookup_datalabels1,lookup_datalabels2. |
|---|
| 721 | \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels1 pi) = |
|---|
| 722 | \fst (assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels2 pi). |
|---|
| 723 | #lookup_labels #sigma #policy #ppc #pi #lookup_datalabels1 #lookup_datalabels2 |
|---|
| 724 | cases pi // |
|---|
| 725 | qed. |
|---|
| 726 | |
|---|
| 727 | lemma fst_snd_assembly_1_pseudoinstruction: |
|---|
| 728 | ∀lookup_labels,sigma,policy,ppc,pi,lookup_datalabels,len,assembled. |
|---|
| 729 | assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi |
|---|
| 730 | = 〈len,assembled〉 → |
|---|
| 731 | len = |assembled|. |
|---|
| 732 | #lookup #sigma #policy #ppc #pi #lookup_datalabels #len #assembled |
|---|
| 733 | inversion (assembly_1_pseudoinstruction ??????) #len' #assembled' |
|---|
| 734 | whd in ⊢ (??%? → ?); #EQ1 #EQ2 destruct % |
|---|
| 735 | qed. |
|---|
| 736 | |
|---|
| 737 | (* XXX: easy but tedious *) |
|---|
| 738 | lemma assembly1_lt_128: |
|---|
| 739 | ∀i: instruction. |
|---|
| 740 | |(assembly1 i)| < 128. |
|---|
| 741 | cases daemon |
|---|
| 742 | (* XXX: commented out as takes ages to type check |
|---|
| 743 | #i cases i |
|---|
| 744 | try (#assm1 #assm2) try #assm1 |
|---|
| 745 | [8: |
|---|
| 746 | cases assm1 |
|---|
| 747 | try (#assm1 #assm2) try #assm1 |
|---|
| 748 | whd in match assembly1; normalize nodelta |
|---|
| 749 | whd in match assembly_preinstruction; normalize nodelta |
|---|
| 750 | try @(subaddressing_mode_elim … assm2) |
|---|
| 751 | try @(subaddressing_mode_elim … assm1) try #w try #w' normalize nodelta |
|---|
| 752 | [32: |
|---|
| 753 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 754 | cases assm1 #addr1 #addr2 normalize nodelta |
|---|
| 755 | [1: |
|---|
| 756 | @(subaddressing_mode_elim … addr2) |
|---|
| 757 | |2: |
|---|
| 758 | @(subaddressing_mode_elim … addr1) |
|---|
| 759 | ] |
|---|
| 760 | #w |
|---|
| 761 | |35,36,37: |
|---|
| 762 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 763 | [1,3: |
|---|
| 764 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 765 | ] |
|---|
| 766 | cases assm1 #addr1 #addr2 normalize nodelta |
|---|
| 767 | @(subaddressing_mode_elim … addr2) try #w |
|---|
| 768 | |49: |
|---|
| 769 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 770 | [1: |
|---|
| 771 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 772 | [1: |
|---|
| 773 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 774 | [1: |
|---|
| 775 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 776 | [1: |
|---|
| 777 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 778 | ] |
|---|
| 779 | ] |
|---|
| 780 | ] |
|---|
| 781 | ] |
|---|
| 782 | cases assm1 #addr1 #addr2 normalize nodelta |
|---|
| 783 | [1,3,4,5: |
|---|
| 784 | @(subaddressing_mode_elim … addr2) try #w |
|---|
| 785 | |*: |
|---|
| 786 | @(subaddressing_mode_elim … addr1) try #w |
|---|
| 787 | normalize nodelta |
|---|
| 788 | [1,2: |
|---|
| 789 | @(subaddressing_mode_elim … addr2) try #w |
|---|
| 790 | ] |
|---|
| 791 | ] |
|---|
| 792 | |50: |
|---|
| 793 | cases assm1 -assm1 #assm1 normalize nodelta |
|---|
| 794 | cases assm1 #addr1 #addr2 normalize nodelta |
|---|
| 795 | [1: |
|---|
| 796 | @(subaddressing_mode_elim … addr2) try #w |
|---|
| 797 | |2: |
|---|
| 798 | @(subaddressing_mode_elim … addr1) try #w |
|---|
| 799 | ] |
|---|
| 800 | ] |
|---|
| 801 | normalize repeat @le_S_S @le_O_n |
|---|
| 802 | ] |
|---|
| 803 | whd in match assembly1; normalize nodelta |
|---|
| 804 | [6: |
|---|
| 805 | normalize repeat @le_S_S @le_O_n |
|---|
| 806 | |7: |
|---|
| 807 | @(subaddressing_mode_elim … assm2) normalize repeat @le_S_S @le_O_n |
|---|
| 808 | |*: |
|---|
| 809 | @(subaddressing_mode_elim … assm1) #w normalize nodelta repeat @le_S_S @le_O_n |
|---|
| 810 | ] |
|---|
| 811 | *) |
|---|
| 812 | qed. |
|---|
| 813 | |
|---|
| 814 | lemma assembly1_pseudoinstruction_lt_2_to_16: |
|---|
| 815 | ∀lookup_labels,sigma,policy,ppc,lookup_datalabels,pi. |
|---|
| 816 | |\snd (assembly_1_pseudoinstruction |
|---|
| 817 | lookup_labels sigma policy ppc lookup_datalabels pi)| |
|---|
| 818 | < 2^16. |
|---|
| 819 | #lookup_labels #sigma #policy #ppc #lookup_datalabels * |
|---|
| 820 | [ cut (128 < 2^16) [@leb_true_to_le %] #LT |
|---|
| 821 | * whd in match (assembly_1_pseudoinstruction ??????); |
|---|
| 822 | whd in match (expand_pseudo_instruction ??????); |
|---|
| 823 | whd in match assembly_1_pseudoinstruction; normalize nodelta |
|---|
| 824 | try (#arg1 #arg2 #arg3) try (#arg1 #arg2) try #arg1 |
|---|
| 825 | whd in match (expand_pseudo_instruction ??????); |
|---|
| 826 | try |
|---|
| 827 | (change with (|flatten ? [assembly1 ?]| < ?) |
|---|
| 828 | >flatten_singleton |
|---|
| 829 | @(transitive_lt … (assembly1_lt_128 ?)) |
|---|
| 830 | @LT) |
|---|
| 831 | @pair_elim #x #y #_ cases x normalize nodelta |
|---|
| 832 | try |
|---|
| 833 | (change with (|flatten ? [assembly1 ?]| < ?) |
|---|
| 834 | >flatten_singleton |
|---|
| 835 | @(transitive_lt … (assembly1_lt_128 ?)) |
|---|
| 836 | @LT) |
|---|
| 837 | change with (|flatten ? [assembly1 ?; assembly1 ?; assembly1 ?]| < ?) |
|---|
| 838 | >length_flatten_cons >length_flatten_cons >length_flatten_cons <plus_n_O |
|---|
| 839 | <associative_plus @(transitive_lt … (tech_transitive_lt_3 … (2^7) ???)) |
|---|
| 840 | try @assembly1_lt_128 @leb_true_to_le % |
|---|
| 841 | |2,3: #msg normalize in ⊢ (?%?); // |
|---|
| 842 | | #label whd in match (assembly_1_pseudoinstruction ??????); |
|---|
| 843 | whd in match (expand_pseudo_instruction ??????); |
|---|
| 844 | @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_ |
|---|
| 845 | [2: @pair_elim #x #y #_ cases (?∧?)] |
|---|
| 846 | normalize in ⊢ (?%?); // |
|---|
| 847 | | #label whd in match (assembly_1_pseudoinstruction ??????); |
|---|
| 848 | whd in match (expand_pseudo_instruction ??????); |
|---|
| 849 | @pair_elim #sj_poss #disp cases (?∧?) normalize nodelta #_ |
|---|
| 850 | normalize in ⊢ (?%?); // |
|---|
| 851 | | #dptr #id normalize in ⊢ (?%?); // |
|---|
| 852 | ] |
|---|
| 853 | qed. |
|---|
| 854 | |
|---|
| 855 | definition assembly: |
|---|
| 856 | ∀p: pseudo_assembly_program. |
|---|
| 857 | ∀sigma: Word → Word. |
|---|
| 858 | ∀policy: Word → bool. |
|---|
| 859 | Σres:list Byte × (BitVectorTrie costlabel 16). |
|---|
| 860 | sigma_policy_specification p sigma policy → |
|---|
| 861 | let 〈preamble,instr_list〉 ≝ p in |
|---|
| 862 | |instr_list| ≤ 2^16 → |
|---|
| 863 | let 〈assembled,costs〉 ≝ res in |
|---|
| 864 | |assembled| ≤ 2^16 ∧ |
|---|
| 865 | (nat_of_bitvector … (sigma (bitvector_of_nat … (|instr_list|))) = |assembled| ∨ |
|---|
| 866 | sigma (bitvector_of_nat … (|instr_list|)) = zero … ∧ |assembled| = 2^16) ∧ |
|---|
| 867 | let 〈labels_to_ppc,ppc_to_costs〉 ≝ create_label_cost_map instr_list in |
|---|
| 868 | let datalabels ≝ construct_datalabels preamble in |
|---|
| 869 | let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in |
|---|
| 870 | let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in |
|---|
| 871 | ∀ppc. ∀ppc_ok:nat_of_bitvector … ppc < |instr_list|. |
|---|
| 872 | let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc ppc_ok in |
|---|
| 873 | let 〈len,assembledi〉 ≝ |
|---|
| 874 | assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels pi in |
|---|
| 875 | |assembledi| ≤ |assembled| ∧ |
|---|
| 876 | ∀j:nat. ∀H: j < |assembledi|. ∀K. |
|---|
| 877 | nth_safe ? j assembledi H = |
|---|
| 878 | nth_safe ? (nat_of_bitvector … (add … (sigma ppc) (bitvector_of_nat ? j))) |
|---|
| 879 | assembled K |
|---|
| 880 | ≝ |
|---|
| 881 | λp. |
|---|
| 882 | λsigma. |
|---|
| 883 | λpolicy. |
|---|
| 884 | deplet 〈preamble, instr_list〉 as p_refl ≝ p in |
|---|
| 885 | deplet 〈labels_to_ppc,ppc_to_costs〉 as eq_create_label_cost_map ≝ create_label_cost_map instr_list in |
|---|
| 886 | let datalabels ≝ construct_datalabels preamble in |
|---|
| 887 | let lookup_labels ≝ λx. bitvector_of_nat ? (lookup_def … labels_to_ppc x 0) in |
|---|
| 888 | let lookup_datalabels ≝ λx. lookup_def … datalabels x (zero ?) in |
|---|
| 889 | let 〈next_pc,revcode〉 ≝ pi1 … ( |
|---|
| 890 | foldl_strong |
|---|
| 891 | (option Identifier × pseudo_instruction) |
|---|
| 892 | (λpre. Σppc_code:(Word × (list Byte)). |
|---|
| 893 | sigma_policy_specification 〈preamble,instr_list〉 sigma policy → |
|---|
| 894 | |instr_list| ≤ 2^16 → |
|---|
| 895 | let 〈ppc,code〉 ≝ ppc_code in |
|---|
| 896 | ppc = bitvector_of_nat … (|pre|) ∧ |
|---|
| 897 | |code| ≤ 2^16 ∧ |
|---|
| 898 | True(*(ppc = zero … → code = [])*) ∧ |
|---|
| 899 | (nat_of_bitvector … (sigma ppc) = |code| ∨ |
|---|
| 900 | sigma ppc = zero … ∧ |code| = 2^16 ∧ |
|---|
| 901 | (|pre| < 2^16 → ∀ppc'. ∀ppc_ok':nat_of_bitvector … ppc' < |instr_list|. nat_of_bitvector … ppc ≤ nat_of_bitvector … ppc' → |
|---|
| 902 | let instruction' ≝ \fst (fetch_pseudo_instruction instr_list ppc' ppc_ok') in |
|---|
| 903 | instruction_size lookup_labels sigma policy ppc' instruction' = 0) |
|---|
| 904 | ) ∧ |
|---|
| 905 | ∀ppc'.∀ppc_ok'. |
|---|
| 906 | (nat_of_bitvector … ppc' < nat_of_bitvector … ppc ∨ |pre| = 2^16) → |
|---|
| 907 | let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' ppc_ok' in |
|---|
| 908 | let 〈len,assembledi〉 ≝ |
|---|
| 909 | assembly_1_pseudoinstruction lookup_labels sigma policy ppc' lookup_datalabels pi in |
|---|
| 910 | |assembledi| ≤ |reverse … code| ∧ |
|---|
| 911 | ∀j:nat. ∀H: j < |assembledi|. ∀K. |
|---|
| 912 | nth_safe ? j assembledi H = |
|---|
| 913 | nth_safe ? (nat_of_bitvector … (add … (sigma ppc') (bitvector_of_nat ? j))) (reverse … code) K) |
|---|
| 914 | instr_list |
|---|
| 915 | (λprefix,hd,tl,prf,ppc_code. |
|---|
| 916 | let 〈ppc, code〉 ≝ pi1 … ppc_code in |
|---|
| 917 | let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction lookup_labels sigma policy ppc lookup_datalabels (\snd hd) in |
|---|
| 918 | let new_ppc ≝ add ? ppc (bitvector_of_nat ? 1) in |
|---|
| 919 | 〈new_ppc, (reverse … program @ code)〉) |
|---|
| 920 | 〈(zero ?), [ ]〉) |
|---|
| 921 | in |
|---|
| 922 | 〈reverse … revcode, |
|---|
| 923 | fold … (λppc.λcost.λpc_to_costs. insert … (sigma ppc) cost pc_to_costs) ppc_to_costs (Stub ??)〉. |
|---|
| 924 | [ cases (foldl_strong ? (λx.Σy.?) ???) in p1; #ignore_revcode #Hfold #EQignore_revcode |
|---|
| 925 | >EQignore_revcode in Hfold; #Hfold #sigma_pol_ok #instr_list_ok |
|---|
| 926 | cases (Hfold sigma_pol_ok instr_list_ok) -Hfold * * * #Hfold1 #Hfold4 #_ #Hfold5 #Hfold3 whd |
|---|
| 927 | <eq_create_label_cost_map whd % |
|---|
| 928 | [2: #ppc #LTppc @Hfold3 >Hfold1 @(eqb_elim (|instr_list|) 2^16) |
|---|
| 929 | [ #limit %2 @limit |
|---|
| 930 | | #nlimit %1 >nat_of_bitvector_bitvector_of_nat_inverse try assumption |
|---|
| 931 | @not_eq_to_le_to_lt assumption ] |
|---|
| 932 | | >length_reverse % try assumption cases Hfold5 -Hfold5 |
|---|
| 933 | [ #Hfold5 <Hfold5 % >Hfold1 % |
|---|
| 934 | | * #Hfold51 #Hfold52 %2 <Hfold1 assumption ]] |
|---|
| 935 | | * #sigma_pol_ok1 #_ #instr_list_ok % |
|---|
| 936 | [ % % [%] // >sigma_pol_ok1 % ] |
|---|
| 937 | #ppc' #ppc_ok' #abs @⊥ cases abs |
|---|
| 938 | [#abs2 cases (not_le_Sn_O ?) [#H @(H abs2) | skip] |
|---|
| 939 | |#abs2 change with (0 = S ?) in abs2; destruct(abs2) ] |
|---|
| 940 | | * #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 |
|---|
| 941 | #IH cases (IH ? instr_list_ok) [2: % assumption ] -IH |
|---|
| 942 | * * * #IH1 #IH2 #IH2' #IH3 #IH4 |
|---|
| 943 | cut (|prefix| < |instr_list|) |
|---|
| 944 | [ >prf >length_append normalize <plus_n_Sm @le_S_S // ] #LT_prefix_instr_list |
|---|
| 945 | cut (|prefix| < 2^16) |
|---|
| 946 | [ @(lt_to_le_to_lt … (|instr_list|)) assumption ] #prefix_ok |
|---|
| 947 | cut (nat_of_bitvector … ppc < |instr_list|) |
|---|
| 948 | [ >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ] #ppc_ok |
|---|
| 949 | cut (\snd hd = \fst (fetch_pseudo_instruction instr_list ppc ppc_ok)) |
|---|
| 950 | [ >prf in ppc_ok; >IH1 >(add_zero … (bitvector_of_nat … (|prefix|))) |
|---|
| 951 | >fetch_pseudo_instruction_append |
|---|
| 952 | [ #ppc_ok whd in match fetch_pseudo_instruction; normalize nodelta |
|---|
| 953 | whd in match (nth_safe ????); [ cases hd // | normalize // ] |
|---|
| 954 | | <add_zero >nat_of_bitvector_bitvector_of_nat_inverse |
|---|
| 955 | [ <prf assumption | assumption ] |
|---|
| 956 | | skip |
|---|
| 957 | | <prf assumption |
|---|
| 958 | ]] #eq_fetch_pseudo_instruction |
|---|
| 959 | lapply (fst_snd_assembly_1_pseudoinstruction … p2) #EQpc_delta |
|---|
| 960 | cut (pc_delta < 2^16) |
|---|
| 961 | [ >EQpc_delta |
|---|
| 962 | @(eq_ind ?? (λp.λ_. |\snd p| < 2^16) ?? p2) |
|---|
| 963 | @assembly1_pseudoinstruction_lt_2_to_16 ] #pc_delta_ok |
|---|
| 964 | cut (pc_delta = instruction_size lookup_labels sigma policy ppc (\snd hd)) |
|---|
| 965 | [ whd in match instruction_size; normalize nodelta |
|---|
| 966 | >fst_assembly_1_pseudoinstruction_insensible_to_lookup_datalabels [ >p2 | skip] % ] |
|---|
| 967 | #EQpc_delta2 |
|---|
| 968 | cases (sigma_pol_ok2 … ppc_ok) |
|---|
| 969 | <eq_fetch_pseudo_instruction <eq_create_label_cost_map <EQpc_delta2 |
|---|
| 970 | #sigma_pol3 #sigma_pol4 |
|---|
| 971 | % [ % [% [% ] ] ] |
|---|
| 972 | [ >length_append normalize nodelta >IH1 @sym_eq @add_bitvector_of_nat |
|---|
| 973 | | >length_append >length_reverse <EQpc_delta |
|---|
| 974 | cases IH3 -IH3 |
|---|
| 975 | [ #IH3 <IH3 >commutative_plus |
|---|
| 976 | cases sigma_pol4 [ #LT @(transitive_le … LT) // | * #_ #EQ >EQ % ] |
|---|
| 977 | | * * #IH3a #IH3b #IH3c >IH3b <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction |
|---|
| 978 | >IH3c try % assumption ] |
|---|
| 979 | | % (*CSC: Stupid proof of True, drop fake invariant *) |
|---|
| 980 | | >length_append >length_reverse |
|---|
| 981 | cases IH3 -IH3 |
|---|
| 982 | [ #IH3 <IH3 <EQpc_delta cases sigma_pol4 |
|---|
| 983 | [ #LT %1 >sigma_pol3 >nat_of_bitvector_add |
|---|
| 984 | [2: >nat_of_bitvector_bitvector_of_nat_inverse assumption] |
|---|
| 985 | >nat_of_bitvector_bitvector_of_nat_inverse try assumption // |
|---|
| 986 | | * #EQ1 #EQ2 %2 % |
|---|
| 987 | [ lapply (eq_f … (bitvector_of_nat 16) … EQ2) <add_bitvector_of_nat_plus |
|---|
| 988 | >bitvector_of_nat_inverse_nat_of_bitvector <sigma_pol3 #X >X % // |
|---|
| 989 | | #LLT_prefix |
|---|
| 990 | cut (S (nat_of_bitvector … ppc) < 2^16) |
|---|
| 991 | [ >length_append in LLT_prefix; <plus_n_Sm <plus_n_O #LLT_prefix |
|---|
| 992 | >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ] |
|---|
| 993 | -LLT_prefix #LLT_prefix |
|---|
| 994 | #ppc' #ppc_ok' #LEQ_newppc_ppc' whd >EQ1 try % |
|---|
| 995 | @(lt_to_le_to_lt … LEQ_newppc_ppc') normalize nodelta |
|---|
| 996 | >nat_of_bitvector_add >nat_of_bitvector_bitvector_of_nat_inverse // ]] |
|---|
| 997 | | * * #IH5 #IH6 #IH7 %2 % [% ] |
|---|
| 998 | [ normalize nodelta >sigma_pol3 >IH5 |
|---|
| 999 | >add_commutative <add_zero >nat_of_bitvector_bitvector_of_nat_inverse try assumption |
|---|
| 1000 | >EQpc_delta2 >eq_fetch_pseudo_instruction >IH7 try % assumption |
|---|
| 1001 | | >IH6 <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction >IH7 try % |
|---|
| 1002 | assumption |
|---|
| 1003 | | #LLT_prefix |
|---|
| 1004 | cut (S (nat_of_bitvector … ppc) < 2^16) |
|---|
| 1005 | [ >length_append in LLT_prefix; <plus_n_Sm <plus_n_O #LLT_prefix |
|---|
| 1006 | >IH1 >nat_of_bitvector_bitvector_of_nat_inverse assumption ] |
|---|
| 1007 | -LLT_prefix #LLT_prefix |
|---|
| 1008 | #ppc' #ppc_ok' #LEQ_newppc_ppc' whd @IH7 try assumption |
|---|
| 1009 | @(transitive_le … LEQ_newppc_ppc') normalize nodelta |
|---|
| 1010 | >nat_of_bitvector_add >nat_of_bitvector_bitvector_of_nat_inverse // ]] |
|---|
| 1011 | | #ppc' #LTppc' cases hd in prf p2 EQpc_delta2 eq_fetch_pseudo_instruction; #label #pi #prf #p2 |
|---|
| 1012 | #EQpc_delta2 #eq_fetch_pseudo_instruction #OR_lt_eq |
|---|
| 1013 | cut (ppc' = ppc ∨ ppc' ≠ ppc) [cases daemon (*CSC: True*)] * |
|---|
| 1014 | [ #EQppc' >EQppc' in LTppc'; -ppc' >prf |
|---|
| 1015 | >IH1 #LTppc lapply LTppc |
|---|
| 1016 | >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → match % with [_ ⇒ ?]); |
|---|
| 1017 | >fetch_pseudo_instruction_append |
|---|
| 1018 | [3: @le_S_S @le_O_n |
|---|
| 1019 | |2: lapply LTppc; >(add_zero … (bitvector_of_nat 16 (|prefix|))) in ⊢ (% → ?); #H @H |
|---|
| 1020 | |4: <prf <p_refl in instr_list_ok; #H @H ] |
|---|
| 1021 | #LTppc' @pair_elim #pi' #newppc' #EQpair destruct(EQpair) <IH1 >p2 % |
|---|
| 1022 | [ >length_reverse >length_append >length_reverse // ] |
|---|
| 1023 | #j #LTj >nat_of_bitvector_add |
|---|
| 1024 | >nat_of_bitvector_bitvector_of_nat_inverse |
|---|
| 1025 | [2,4: @(lt_to_le_to_lt … LTj) <EQpc_delta @(transitive_le … pc_delta_ok) %2 % |
|---|
| 1026 | |3: @(lt_to_le_to_lt … (nat_of_bitvector … (sigma ppc) + pc_delta)) |
|---|
| 1027 | [ >EQpc_delta @monotonic_lt_plus_r assumption |
|---|
| 1028 | | cases sigma_pol4 |
|---|
| 1029 | [ #H @(transitive_le … H) %2 % |
|---|
| 1030 | | * #_ #EQ >EQ % ]]] |
|---|
| 1031 | >reverse_append >reverse_reverse |
|---|
| 1032 | cases IH3 -IH3 |
|---|
| 1033 | [ #IH3 >IH3 <(length_reverse … code) @nth_safe_prepend |
|---|
| 1034 | | * * #IH3a #IH3b #IH3c >IH3a @⊥ |
|---|
| 1035 | cut (|program| = 0) |
|---|
| 1036 | [ <EQpc_delta >EQpc_delta2 >eq_fetch_pseudo_instruction @IH3c // ] #EQprogram |
|---|
| 1037 | @(absurd ?? (not_le_Sn_O j)) <EQprogram assumption ] |
|---|
| 1038 | | #NEQppc' |
|---|
| 1039 | lapply (IH4 … LTppc') |
|---|
| 1040 | @pair_elim #pi' #newppc' #eq_fetch_pseudoinstruction |
|---|
| 1041 | @pair_elim #len' #assembledi' #eq_assembly_1_pseudoinstruction #IH |
|---|
| 1042 | cases (IH ?) -IH |
|---|
| 1043 | [2: %1 cases OR_lt_eq |
|---|
| 1044 | [ normalize nodelta >nat_of_bitvector_add |
|---|
| 1045 | [2: cases daemon (*CSC: should be true*)] |
|---|
| 1046 | >nat_of_bitvector_bitvector_of_nat_inverse [2: // ] |
|---|
| 1047 | <plus_n_Sm <plus_n_O #X lapply (le_S_S_to_le … X) -X #X |
|---|
| 1048 | cases (le_to_or_lt_eq … X) [//] #abs @⊥ |
|---|
| 1049 | lapply (eq_f … (bitvector_of_nat 16) … abs) |
|---|
| 1050 | >bitvector_of_nat_inverse_nat_of_bitvector |
|---|
| 1051 | >bitvector_of_nat_inverse_nat_of_bitvector #EQ |
|---|
| 1052 | @(absurd … EQ NEQppc') |
|---|
| 1053 | | >length_append <plus_n_Sm <plus_n_O #EQ @le_S_S_to_le >IH1 |
|---|
| 1054 | >nat_of_bitvector_bitvector_of_nat_inverse try assumption |
|---|
| 1055 | cases (le_to_or_lt_eq … (lt_nat_of_bitvector 16 ppc')) [#X >EQ @X] |
|---|
| 1056 | #abs @⊥ <EQ in abs; #X lapply (injective_S … X) #abs |
|---|
| 1057 | lapply (eq_f … (bitvector_of_nat 16) … abs) |
|---|
| 1058 | >bitvector_of_nat_inverse_nat_of_bitvector <IH1 #EQ |
|---|
| 1059 | @(absurd … EQ NEQppc') ]] |
|---|
| 1060 | #IH6 #IH |
|---|
| 1061 | change with (let 〈len,assembledi〉 ≝ assembly_1_pseudoinstruction ????? pi' in ? ∧ ∀j:ℕ. ∀H:j<|assembledi|.?) |
|---|
| 1062 | >eq_assembly_1_pseudoinstruction % |
|---|
| 1063 | [ >reverse_append >length_append |
|---|
| 1064 | >(fst_snd_assembly_1_pseudoinstruction … eq_assembly_1_pseudoinstruction) |
|---|
| 1065 | @(transitive_le … IH6) // |
|---|
| 1066 | | #j #LTj >reverse_append >reverse_reverse #K |
|---|
| 1067 | >IH |
|---|
| 1068 | [2: >length_reverse (*CSC: ?????????? <IH3 (*CSC: USE MONOTONICITY *)*) cases daemon |
|---|
| 1069 | | @shift_nth_prefix ]]]]] |
|---|
| 1070 | qed. |
|---|
| 1071 | |
|---|
| 1072 | definition assembly_unlabelled_program: |
|---|
| 1073 | assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝ |
|---|
| 1074 | λp. |
|---|
| 1075 | Some … (〈foldr … (λi,l. assembly1 i @ l) [ ] p, Stub …〉). |
|---|