[719] | 1 | include "ASM/ASM.ma". |
---|
| 2 | include "ASM/BitVectorTrie.ma". |
---|
| 3 | include "ASM/Arithmetic.ma". |
---|
| 4 | include "ASM/Fetch.ma". |
---|
| 5 | include "ASM/Status.ma". |
---|
| 6 | |
---|
[820] | 7 | definition assembly_preinstruction ≝ |
---|
| 8 | λA: Type[0]. |
---|
| 9 | λaddr_of: A → Byte. (* relative *) |
---|
| 10 | λpre: preinstruction A. |
---|
| 11 | match pre with |
---|
| 12 | [ ADD addr1 addr2 ⇒ |
---|
[757] | 13 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 14 | [ REGISTER r ⇒ λ_.[ ([[false;false;true;false;true]]) @@ r ] |
---|
| 15 | | DIRECT b1 ⇒ λ_.[ ([[false;false;true;false;false;true;false;true]]); b1 ] |
---|
| 16 | | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;false;false;true;true;i1]]) ] |
---|
| 17 | | DATA b1 ⇒ λ_. [ ([[false;false;true;false;false;true;false;false]]) ; b1 ] |
---|
| 18 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 19 | | ADDC addr1 addr2 ⇒ |
---|
[757] | 20 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 21 | [ REGISTER r ⇒ λ_.[ ([[false;false;true;true;true]]) @@ r ] |
---|
| 22 | | DIRECT b1 ⇒ λ_.[ ([[false;false;true;true;false;true;false;true]]); b1 ] |
---|
| 23 | | INDIRECT i1 ⇒ λ_. [ ([[false;false;true;true;false;true;true;i1]]) ] |
---|
| 24 | | DATA b1 ⇒ λ_. [ ([[false;false;true;true;false;true;false;false]]) ; b1 ] |
---|
| 25 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 26 | | ANL addrs ⇒ |
---|
| 27 | match addrs with |
---|
| 28 | [ inl addrs ⇒ match addrs with |
---|
| 29 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 30 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 31 | [ REGISTER r ⇒ λ_.[ ([[false;true;false;true;true]]) @@ r ] |
---|
| 32 | | DIRECT b1 ⇒ λ_.[ ([[false;true;false;true;false;true;false;true]]); b1 ] |
---|
| 33 | | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;true;false;true;true;i1]]) ] |
---|
| 34 | | DATA b1 ⇒ λ_. [ ([[false;true;false;true;false;true;false;false]]) ; b1 ] |
---|
| 35 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 36 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 37 | let b1 ≝ |
---|
| 38 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 39 | [ DIRECT b1 ⇒ λ_.b1 |
---|
| 40 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 41 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 42 | [ ACC_A ⇒ λ_.[ ([[false;true;false;true;false;false;true;false]]) ; b1 ] |
---|
| 43 | | DATA b2 ⇒ λ_. [ ([[false;true;false;true;false;false;true;true]]) ; b1 ; b2 ] |
---|
| 44 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 45 | ] |
---|
| 46 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 47 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with |
---|
| 48 | [ BIT_ADDR b1 ⇒ λ_.[ ([[true;false;false;false;false;false;true;false]]) ; b1 ] |
---|
| 49 | | N_BIT_ADDR b1 ⇒ λ_. [ ([[true;false;true;true;false;false;false;false]]) ; b1 ] |
---|
| 50 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 51 | | CLR addr ⇒ |
---|
| 52 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with |
---|
| 53 | [ ACC_A ⇒ λ_. |
---|
| 54 | [ ([[true; true; true; false; false; true; false; false]]) ] |
---|
| 55 | | CARRY ⇒ λ_. |
---|
| 56 | [ ([[true; true; false; false; false; false; true; true]]) ] |
---|
| 57 | | BIT_ADDR b1 ⇒ λ_. |
---|
| 58 | [ ([[true; true; false; false; false; false; true; false]]) ; b1 ] |
---|
| 59 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 60 | | CPL addr ⇒ |
---|
| 61 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;carry;bit_addr]] x) → ? with |
---|
| 62 | [ ACC_A ⇒ λ_. |
---|
| 63 | [ ([[true; true; true; true; false; true; false; false]]) ] |
---|
| 64 | | CARRY ⇒ λ_. |
---|
| 65 | [ ([[true; false; true; true; false; false; true; true]]) ] |
---|
| 66 | | BIT_ADDR b1 ⇒ λ_. |
---|
| 67 | [ ([[true; false; true; true; false; false; true; false]]) ; b1 ] |
---|
| 68 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 69 | | DA addr ⇒ |
---|
| 70 | [ ([[true; true; false; true; false; true; false; false]]) ] |
---|
| 71 | | DEC addr ⇒ |
---|
[757] | 72 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect]] x) → ? with |
---|
[719] | 73 | [ ACC_A ⇒ λ_. |
---|
| 74 | [ ([[false; false; false; true; false; true; false; false]]) ] |
---|
| 75 | | REGISTER r ⇒ λ_. |
---|
| 76 | [ ([[false; false; false; true; true]]) @@ r ] |
---|
| 77 | | DIRECT b1 ⇒ λ_. |
---|
| 78 | [ ([[false; false; false; true; false; true; false; true]]); b1 ] |
---|
| 79 | | INDIRECT i1 ⇒ λ_. |
---|
| 80 | [ ([[false; false; false; true; false; true; true; i1]]) ] |
---|
| 81 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
[820] | 82 | | DJNZ addr1 addr2 ⇒ |
---|
| 83 | let b2 ≝ addr_of addr2 in |
---|
[757] | 84 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;direct]] x) → ? with |
---|
[719] | 85 | [ REGISTER r ⇒ λ_. |
---|
| 86 | [ ([[true; true; false; true; true]]) @@ r ; b2 ] |
---|
| 87 | | DIRECT b1 ⇒ λ_. |
---|
| 88 | [ ([[true; true; false; true; false; true; false; true]]); b1; b2 ] |
---|
| 89 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
| 90 | | JC addr ⇒ |
---|
[820] | 91 | let b1 ≝ addr_of addr in |
---|
| 92 | [ ([[false; true; false; false; false; false; false; false]]); b1 ] |
---|
[719] | 93 | | JNC addr ⇒ |
---|
[820] | 94 | let b1 ≝ addr_of addr in |
---|
| 95 | [ ([[false; true; false; true; false; false; false; false]]); b1 ] |
---|
[719] | 96 | | JZ addr ⇒ |
---|
[820] | 97 | let b1 ≝ addr_of addr in |
---|
| 98 | [ ([[false; true; true; false; false; false; false; false]]); b1 ] |
---|
[719] | 99 | | JNZ addr ⇒ |
---|
[820] | 100 | let b1 ≝ addr_of addr in |
---|
| 101 | [ ([[false; true; true; true; false; false; false; false]]); b1 ] |
---|
[719] | 102 | | JB addr1 addr2 ⇒ |
---|
[820] | 103 | let b2 ≝ addr_of addr2 in |
---|
[719] | 104 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 105 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 106 | [ ([[false; false; true; false; false; false; false; false]]); b1; b2 ] |
---|
| 107 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
| 108 | | JNB addr1 addr2 ⇒ |
---|
[820] | 109 | let b2 ≝ addr_of addr2 in |
---|
[719] | 110 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 111 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 112 | [ ([[false; false; true; true; false; false; false; false]]); b1; b2 ] |
---|
| 113 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
| 114 | | JBC addr1 addr2 ⇒ |
---|
[820] | 115 | let b2 ≝ addr_of addr2 in |
---|
[719] | 116 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 117 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 118 | [ ([[false; false; false; true; false; false; false; false]]); b1; b2 ] |
---|
| 119 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
| 120 | | CJNE addrs addr3 ⇒ |
---|
[820] | 121 | let b3 ≝ addr_of addr3 in |
---|
[719] | 122 | match addrs with |
---|
| 123 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 124 | match addr2 return λx. bool_to_Prop (is_in ? [[direct;data]] x) → ? with |
---|
| 125 | [ DIRECT b1 ⇒ λ_. |
---|
| 126 | [ ([[true; false; true; true; false; true; false; true]]); b1; b3 ] |
---|
| 127 | | DATA b1 ⇒ λ_. |
---|
| 128 | [ ([[true; false; true; true; false; true; false; false]]); b1; b3 ] |
---|
| 129 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 130 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 131 | let b2 ≝ |
---|
| 132 | match addr2 return λx. bool_to_Prop (is_in ? [[data]] x) → ? with |
---|
| 133 | [ DATA b2 ⇒ λ_. b2 |
---|
| 134 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) in |
---|
[757] | 135 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → list Byte with |
---|
[820] | 136 | [ REGISTER r ⇒ λ_. |
---|
[719] | 137 | [ ([[true; false; true; true; true]]) @@ r; b2; b3 ] |
---|
| 138 | | INDIRECT i1 ⇒ λ_. |
---|
| 139 | [ ([[true; false; true; true; false; true; true; i1]]); b2; b3 ] |
---|
[820] | 140 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) |
---|
| 141 | ] |
---|
| 142 | | DIV addr1 addr2 ⇒ |
---|
| 143 | [ ([[true;false;false;false;false;true;false;false]]) ] |
---|
[719] | 144 | | INC addr ⇒ |
---|
[757] | 145 | match addr return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;dptr]] x) → ? with |
---|
[719] | 146 | [ ACC_A ⇒ λ_. |
---|
| 147 | [ ([[false;false;false;false;false;true;false;false]]) ] |
---|
| 148 | | REGISTER r ⇒ λ_. |
---|
| 149 | [ ([[false;false;false;false;true]]) @@ r ] |
---|
| 150 | | DIRECT b1 ⇒ λ_. |
---|
| 151 | [ ([[false; false; false; false; false; true; false; true]]); b1 ] |
---|
| 152 | | INDIRECT i1 ⇒ λ_. |
---|
| 153 | [ ([[false; false; false; false; false; true; true; i1]]) ] |
---|
| 154 | | DPTR ⇒ λ_. |
---|
| 155 | [ ([[true;false;true;false;false;false;true;true]]) ] |
---|
| 156 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 157 | | MOV addrs ⇒ |
---|
| 158 | match addrs with |
---|
| 159 | [ inl addrs ⇒ |
---|
| 160 | match addrs with |
---|
| 161 | [ inl addrs ⇒ |
---|
| 162 | match addrs with |
---|
| 163 | [ inl addrs ⇒ |
---|
| 164 | match addrs with |
---|
| 165 | [ inl addrs ⇒ |
---|
| 166 | match addrs with |
---|
| 167 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 168 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 169 | [ REGISTER r ⇒ λ_.[ ([[true;true;true;false;true]]) @@ r ] |
---|
| 170 | | DIRECT b1 ⇒ λ_.[ ([[true;true;true;false;false;true;false;true]]); b1 ] |
---|
| 171 | | INDIRECT i1 ⇒ λ_. [ ([[true;true;true;false;false;true;true;i1]]) ] |
---|
| 172 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;false;false]]) ; b1 ] |
---|
| 173 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 174 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 175 | match addr1 return λx. bool_to_Prop (is_in ? [[registr;indirect]] x) → ? with |
---|
[719] | 176 | [ REGISTER r ⇒ λ_. |
---|
| 177 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with |
---|
| 178 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;true]]) @@ r ] |
---|
| 179 | | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;true]]) @@ r; b1 ] |
---|
| 180 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;true]]) @@ r; b1 ] |
---|
| 181 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 182 | | INDIRECT i1 ⇒ λ_. |
---|
| 183 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;direct;data]] x) → ? with |
---|
| 184 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;true;i1]]) ] |
---|
| 185 | | DIRECT b1 ⇒ λ_.[ ([[true;false;true;false;false;true;true;i1]]); b1 ] |
---|
| 186 | | DATA b1 ⇒ λ_. [ ([[false;true;true;true;false;true;true;i1]]) ; b1 ] |
---|
| 187 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 188 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 189 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 190 | let b1 ≝ |
---|
| 191 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 192 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 193 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
[757] | 194 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;registr;direct;indirect;data]] x) → ? with |
---|
[719] | 195 | [ ACC_A ⇒ λ_.[ ([[true;true;true;true;false;true;false;true]]); b1] |
---|
| 196 | | REGISTER r ⇒ λ_.[ ([[true;false;false;false;true]]) @@ r; b1 ] |
---|
| 197 | | DIRECT b2 ⇒ λ_.[ ([[true;false;false;false;false;true;false;true]]); b1; b2 ] |
---|
| 198 | | INDIRECT i1 ⇒ λ_. [ ([[true;false;false;false;false;true;true;i1]]); b1 ] |
---|
| 199 | | DATA b2 ⇒ λ_. [ ([[false;true;true;true;false;true;false;true]]); b1; b2 ] |
---|
| 200 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 201 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 202 | match addr2 return λx. bool_to_Prop (is_in ? [[data16]] x) → ? with |
---|
| 203 | [ DATA16 w ⇒ λ_. |
---|
[938] | 204 | let b1_b2 ≝ split ? 8 8 w in |
---|
| 205 | let b1 ≝ \fst b1_b2 in |
---|
[1040] | 206 | let b2 ≝ \snd b1_b2 in |
---|
[719] | 207 | [ ([[true;false;false;true;false;false;false;false]]); b1; b2] |
---|
| 208 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 209 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 210 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 211 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 212 | [ ([[true;false;true;false;false;false;true;false]]); b1 ] |
---|
| 213 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 214 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 215 | match addr1 return λx. bool_to_Prop (is_in ? [[bit_addr]] x) → ? with |
---|
| 216 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 217 | [ ([[true;false;false;true;false;false;true;false]]); b1 ] |
---|
| 218 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 219 | | MOVX addrs ⇒ |
---|
| 220 | match addrs with |
---|
| 221 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 222 | match addr2 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
| 223 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
| 224 | [ ([[true;true;true;false;false;false;true;i1]]) ] |
---|
| 225 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
| 226 | [ ([[true;true;true;false;false;false;false;false]]) ] |
---|
| 227 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 228 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 229 | match addr1 return λx. bool_to_Prop (is_in ? [[ext_indirect;ext_indirect_dptr]] x) → ? with |
---|
| 230 | [ EXT_INDIRECT i1 ⇒ λ_. |
---|
| 231 | [ ([[true;true;true;true;false;false;true;i1]]) ] |
---|
| 232 | | EXT_INDIRECT_DPTR ⇒ λ_. |
---|
| 233 | [ ([[true;true;true;true;false;false;false;false]]) ] |
---|
| 234 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1)] |
---|
| 235 | | MUL addr1 addr2 ⇒ |
---|
| 236 | [ ([[true;false;true;false;false;true;false;false]]) ] |
---|
| 237 | | NOP ⇒ |
---|
| 238 | [ ([[false;false;false;false;false;false;false;false]]) ] |
---|
| 239 | | ORL addrs ⇒ |
---|
| 240 | match addrs with |
---|
| 241 | [ inl addrs ⇒ |
---|
| 242 | match addrs with |
---|
| 243 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 244 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;data;direct;indirect]] x) → ? with |
---|
[719] | 245 | [ REGISTER r ⇒ λ_.[ ([[false;true;false;false;true]]) @@ r ] |
---|
| 246 | | DIRECT b1 ⇒ λ_.[ ([[false;true;false;false;false;true;false;true]]); b1 ] |
---|
| 247 | | INDIRECT i1 ⇒ λ_. [ ([[false;true;false;false;false;true;true;i1]]) ] |
---|
| 248 | | DATA b1 ⇒ λ_. [ ([[false;true;false;false;false;true;false;false]]) ; b1 ] |
---|
| 249 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 250 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 251 | let b1 ≝ |
---|
| 252 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 253 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 254 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 255 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 256 | [ ACC_A ⇒ λ_. |
---|
| 257 | [ ([[false;true;false;false;false;false;true;false]]); b1 ] |
---|
| 258 | | DATA b2 ⇒ λ_. |
---|
| 259 | [ ([[false;true;false;false;false;false;true;true]]); b1; b2 ] |
---|
| 260 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 261 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 262 | match addr2 return λx. bool_to_Prop (is_in ? [[bit_addr;n_bit_addr]] x) → ? with |
---|
| 263 | [ BIT_ADDR b1 ⇒ λ_. |
---|
| 264 | [ ([[false;true;true;true;false;false;true;false]]); b1 ] |
---|
| 265 | | N_BIT_ADDR b1 ⇒ λ_. |
---|
| 266 | [ ([[true;false;true;false;false;false;false;false]]); b1 ] |
---|
| 267 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 268 | | POP addr ⇒ |
---|
| 269 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 270 | [ DIRECT b1 ⇒ λ_. |
---|
| 271 | [ ([[true;true;false;true;false;false;false;false]]) ; b1 ] |
---|
| 272 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 273 | | PUSH addr ⇒ |
---|
| 274 | match addr return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 275 | [ DIRECT b1 ⇒ λ_. |
---|
| 276 | [ ([[true;true;false;false;false;false;false;false]]) ; b1 ] |
---|
| 277 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 278 | | RET ⇒ |
---|
| 279 | [ ([[false;false;true;false;false;false;true;false]]) ] |
---|
| 280 | | RETI ⇒ |
---|
| 281 | [ ([[false;false;true;true;false;false;true;false]]) ] |
---|
| 282 | | RL addr ⇒ |
---|
| 283 | [ ([[false;false;true;false;false;false;true;true]]) ] |
---|
| 284 | | RLC addr ⇒ |
---|
| 285 | [ ([[false;false;true;true;false;false;true;true]]) ] |
---|
| 286 | | RR addr ⇒ |
---|
| 287 | [ ([[false;false;false;false;false;false;true;true]]) ] |
---|
| 288 | | RRC addr ⇒ |
---|
| 289 | [ ([[false;false;false;true;false;false;true;true]]) ] |
---|
| 290 | | SETB addr ⇒ |
---|
| 291 | match addr return λx. bool_to_Prop (is_in ? [[carry;bit_addr]] x) → ? with |
---|
| 292 | [ CARRY ⇒ λ_. |
---|
| 293 | [ ([[true;true;false;true;false;false;true;true]]) ] |
---|
| 294 | | BIT_ADDR b1 ⇒ λ_. |
---|
| 295 | [ ([[true;true;false;true;false;false;true;false]]); b1 ] |
---|
| 296 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 297 | | SUBB addr1 addr2 ⇒ |
---|
[757] | 298 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect;data]] x) → ? with |
---|
[719] | 299 | [ REGISTER r ⇒ λ_. |
---|
| 300 | [ ([[true;false;false;true;true]]) @@ r ] |
---|
| 301 | | DIRECT b1 ⇒ λ_. |
---|
| 302 | [ ([[true;false;false;true;false;true;false;true]]); b1] |
---|
| 303 | | INDIRECT i1 ⇒ λ_. |
---|
| 304 | [ ([[true;false;false;true;false;true;true;i1]]) ] |
---|
| 305 | | DATA b1 ⇒ λ_. |
---|
| 306 | [ ([[true;false;false;true;false;true;false;false]]); b1] |
---|
| 307 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 308 | | SWAP addr ⇒ |
---|
| 309 | [ ([[true;true;false;false;false;true;false;false]]) ] |
---|
| 310 | | XCH addr1 addr2 ⇒ |
---|
[757] | 311 | match addr2 return λx. bool_to_Prop (is_in ? [[registr;direct;indirect]] x) → ? with |
---|
[719] | 312 | [ REGISTER r ⇒ λ_. |
---|
| 313 | [ ([[true;true;false;false;true]]) @@ r ] |
---|
| 314 | | DIRECT b1 ⇒ λ_. |
---|
| 315 | [ ([[true;true;false;false;false;true;false;true]]); b1] |
---|
| 316 | | INDIRECT i1 ⇒ λ_. |
---|
| 317 | [ ([[true;true;false;false;false;true;true;i1]]) ] |
---|
| 318 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 319 | | XCHD addr1 addr2 ⇒ |
---|
| 320 | match addr2 return λx. bool_to_Prop (is_in ? [[indirect]] x) → ? with |
---|
| 321 | [ INDIRECT i1 ⇒ λ_. |
---|
| 322 | [ ([[true;true;false;true;false;true;true;i1]]) ] |
---|
| 323 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 324 | | XRL addrs ⇒ |
---|
| 325 | match addrs with |
---|
| 326 | [ inl addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
[757] | 327 | match addr2 return λx. bool_to_Prop (is_in ? [[data;registr;direct;indirect]] x) → ? with |
---|
[719] | 328 | [ REGISTER r ⇒ λ_. |
---|
| 329 | [ ([[false;true;true;false;true]]) @@ r ] |
---|
| 330 | | DIRECT b1 ⇒ λ_. |
---|
| 331 | [ ([[false;true;true;false;false;true;false;true]]); b1] |
---|
| 332 | | INDIRECT i1 ⇒ λ_. |
---|
| 333 | [ ([[false;true;true;false;false;true;true;i1]]) ] |
---|
| 334 | | DATA b1 ⇒ λ_. |
---|
| 335 | [ ([[false;true;true;false;false;true;false;false]]); b1] |
---|
| 336 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 337 | | inr addrs ⇒ let 〈addr1,addr2〉 ≝ addrs in |
---|
| 338 | let b1 ≝ |
---|
| 339 | match addr1 return λx. bool_to_Prop (is_in ? [[direct]] x) → ? with |
---|
| 340 | [ DIRECT b1 ⇒ λ_. b1 |
---|
| 341 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr1) in |
---|
| 342 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_a;data]] x) → ? with |
---|
| 343 | [ ACC_A ⇒ λ_. |
---|
| 344 | [ ([[false;true;true;false;false;false;true;false]]); b1 ] |
---|
| 345 | | DATA b2 ⇒ λ_. |
---|
| 346 | [ ([[false;true;true;false;false;false;true;true]]); b1; b2 ] |
---|
[820] | 347 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2)] |
---|
| 348 | ]. |
---|
[719] | 349 | |
---|
[820] | 350 | definition assembly1 ≝ |
---|
| 351 | λi: instruction. |
---|
| 352 | match i with |
---|
| 353 | [ ACALL addr ⇒ |
---|
| 354 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
| 355 | [ ADDR11 w ⇒ λ_. |
---|
[938] | 356 | let v1_v2 ≝ split ? 3 8 w in |
---|
| 357 | let v1 ≝ \fst v1_v2 in |
---|
| 358 | let v2 ≝ \snd v1_v2 in |
---|
[820] | 359 | [ (v1 @@ [[true; false; false; false; true]]) ; v2 ] |
---|
| 360 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 361 | | AJMP addr ⇒ |
---|
| 362 | match addr return λx. bool_to_Prop (is_in ? [[addr11]] x) → ? with |
---|
| 363 | [ ADDR11 w ⇒ λ_. |
---|
[938] | 364 | let v1_v2 ≝ split ? 3 8 w in |
---|
| 365 | let v1 ≝ \fst v1_v2 in |
---|
| 366 | let v2 ≝ \snd v1_v2 in |
---|
[820] | 367 | [ (v1 @@ [[false; false; false; false; true]]) ; v2 ] |
---|
| 368 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 369 | | JMP adptr ⇒ |
---|
| 370 | [ ([[false;true;true;true;false;false;true;true]]) ] |
---|
| 371 | | LCALL addr ⇒ |
---|
| 372 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
---|
| 373 | [ ADDR16 w ⇒ λ_. |
---|
[938] | 374 | let b1_b2 ≝ split ? 8 8 w in |
---|
| 375 | let b1 ≝ \fst b1_b2 in |
---|
| 376 | let b2 ≝ \snd b1_b2 in |
---|
[820] | 377 | [ ([[false;false;false;true;false;false;true;false]]); b1; b2 ] |
---|
| 378 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 379 | | LJMP addr ⇒ |
---|
| 380 | match addr return λx. bool_to_Prop (is_in ? [[addr16]] x) → ? with |
---|
| 381 | [ ADDR16 w ⇒ λ_. |
---|
[938] | 382 | let b1_b2 ≝ split ? 8 8 w in |
---|
| 383 | let b1 ≝ \fst b1_b2 in |
---|
| 384 | let b2 ≝ \snd b1_b2 in |
---|
[820] | 385 | [ ([[false;false;false;false;false;false;true;false]]); b1; b2 ] |
---|
| 386 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 387 | | MOVC addr1 addr2 ⇒ |
---|
| 388 | match addr2 return λx. bool_to_Prop (is_in ? [[acc_dptr;acc_pc]] x) → ? with |
---|
| 389 | [ ACC_DPTR ⇒ λ_. |
---|
| 390 | [ ([[true;false;false;true;false;false;true;true]]) ] |
---|
| 391 | | ACC_PC ⇒ λ_. |
---|
| 392 | [ ([[true;false;false;false;false;false;true;true]]) ] |
---|
| 393 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr2) |
---|
| 394 | | SJMP addr ⇒ |
---|
| 395 | match addr return λx. bool_to_Prop (is_in ? [[relative]] x) → ? with |
---|
| 396 | [ RELATIVE b1 ⇒ λ_. |
---|
| 397 | [ ([[true;false;false;false;false;false;false;false]]); b1 ] |
---|
| 398 | | _ ⇒ λK.match K in False with [ ] ] (subaddressing_modein … addr) |
---|
| 399 | | RealInstruction instr ⇒ |
---|
| 400 | assembly_preinstruction [[ relative ]] |
---|
| 401 | (λx. |
---|
| 402 | match x return λs. bool_to_Prop (is_in ? [[ relative ]] s) → ? with |
---|
| 403 | [ RELATIVE r ⇒ λ_. r |
---|
| 404 | | _ ⇒ λabsd. ⊥ |
---|
| 405 | ] (subaddressing_modein … x)) instr |
---|
| 406 | ]. |
---|
| 407 | cases absd |
---|
| 408 | qed. |
---|
| 409 | |
---|
[832] | 410 | inductive jump_length: Type[0] ≝ |
---|
| 411 | | short_jump: jump_length |
---|
| 412 | | medium_jump: jump_length |
---|
| 413 | | long_jump: jump_length. |
---|
| 414 | |
---|
[1309] | 415 | (* jump_expansion_policy: instruction number ↦ 〈pc, jump_length〉 *) |
---|
[1393] | 416 | definition jump_expansion_policy ≝ BitVectorTrie (ℕ × jump_length) 16. |
---|
[907] | 417 | |
---|
[842] | 418 | definition expand_relative_jump_internal: |
---|
| 419 | (Identifier → Word) → jump_length → Identifier → Word → ([[relative]] → preinstruction [[relative]]) → |
---|
| 420 | option (list instruction) |
---|
| 421 | ≝ |
---|
| 422 | λlookup_labels,jmp_len.λjmp:Identifier.λpc,i. |
---|
[832] | 423 | match jmp_len with |
---|
[842] | 424 | [ short_jump ⇒ |
---|
| 425 | let lookup_address ≝ lookup_labels jmp in |
---|
| 426 | let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in |
---|
| 427 | let 〈upper, lower〉 ≝ split ? 8 8 result in |
---|
| 428 | if eq_bv ? upper (zero 8) then |
---|
| 429 | let address ≝ RELATIVE lower in |
---|
| 430 | Some ? [ RealInstruction (i address) ] |
---|
| 431 | else |
---|
| 432 | None ? |
---|
| 433 | | medium_jump ⇒ None … |
---|
[832] | 434 | | long_jump ⇒ |
---|
[842] | 435 | Some ? |
---|
| 436 | [ RealInstruction (i (RELATIVE (bitvector_of_nat ? 2))); |
---|
[832] | 437 | SJMP (RELATIVE (bitvector_of_nat ? 3)); (* LJMP size? *) |
---|
[842] | 438 | LJMP (ADDR16 (lookup_labels jmp)) |
---|
[832] | 439 | ] |
---|
| 440 | ]. |
---|
| 441 | @ I |
---|
| 442 | qed. |
---|
[825] | 443 | |
---|
[842] | 444 | definition expand_relative_jump: (Identifier → Word) → jump_length → Word → preinstruction Identifier → option (list instruction) ≝ |
---|
| 445 | λlookup_labels. |
---|
[832] | 446 | λjmp_len: jump_length. |
---|
[842] | 447 | λpc. |
---|
[832] | 448 | λi: preinstruction Identifier. |
---|
| 449 | let rel_jmp ≝ RELATIVE (bitvector_of_nat ? 2) in |
---|
| 450 | match i with |
---|
[842] | 451 | [ JC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JC ?) |
---|
| 452 | | JNC jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNC ?) |
---|
| 453 | | JB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JB ? baddr) |
---|
| 454 | | JZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JZ ?) |
---|
| 455 | | JNZ jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNZ ?) |
---|
| 456 | | JBC baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JBC ? baddr) |
---|
| 457 | | JNB baddr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (JNB ? baddr) |
---|
| 458 | | CJNE addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (CJNE ? addr) |
---|
| 459 | | DJNZ addr jmp ⇒ expand_relative_jump_internal lookup_labels jmp_len jmp pc (DJNZ ? addr) |
---|
[832] | 460 | | ADD arg1 arg2 ⇒ Some ? [ ADD ? arg1 arg2 ] |
---|
| 461 | | ADDC arg1 arg2 ⇒ Some ? [ ADDC ? arg1 arg2 ] |
---|
| 462 | | SUBB arg1 arg2 ⇒ Some ? [ SUBB ? arg1 arg2 ] |
---|
| 463 | | INC arg ⇒ Some ? [ INC ? arg ] |
---|
| 464 | | DEC arg ⇒ Some ? [ DEC ? arg ] |
---|
| 465 | | MUL arg1 arg2 ⇒ Some ? [ MUL ? arg1 arg2 ] |
---|
| 466 | | DIV arg1 arg2 ⇒ Some ? [ DIV ? arg1 arg2 ] |
---|
| 467 | | DA arg ⇒ Some ? [ DA ? arg ] |
---|
| 468 | | ANL arg ⇒ Some ? [ ANL ? arg ] |
---|
| 469 | | ORL arg ⇒ Some ? [ ORL ? arg ] |
---|
| 470 | | XRL arg ⇒ Some ? [ XRL ? arg ] |
---|
| 471 | | CLR arg ⇒ Some ? [ CLR ? arg ] |
---|
| 472 | | CPL arg ⇒ Some ? [ CPL ? arg ] |
---|
| 473 | | RL arg ⇒ Some ? [ RL ? arg ] |
---|
| 474 | | RR arg ⇒ Some ? [ RR ? arg ] |
---|
| 475 | | RLC arg ⇒ Some ? [ RLC ? arg ] |
---|
| 476 | | RRC arg ⇒ Some ? [ RRC ? arg ] |
---|
| 477 | | SWAP arg ⇒ Some ? [ SWAP ? arg ] |
---|
| 478 | | MOV arg ⇒ Some ? [ MOV ? arg ] |
---|
| 479 | | MOVX arg ⇒ Some ? [ MOVX ? arg ] |
---|
| 480 | | SETB arg ⇒ Some ? [ SETB ? arg ] |
---|
| 481 | | PUSH arg ⇒ Some ? [ PUSH ? arg ] |
---|
| 482 | | POP arg ⇒ Some ? [ POP ? arg ] |
---|
| 483 | | XCH arg1 arg2 ⇒ Some ? [ XCH ? arg1 arg2 ] |
---|
| 484 | | XCHD arg1 arg2 ⇒ Some ? [ XCHD ? arg1 arg2 ] |
---|
| 485 | | RET ⇒ Some ? [ RET ? ] |
---|
| 486 | | RETI ⇒ Some ? [ RETI ? ] |
---|
| 487 | | NOP ⇒ Some ? [ RealInstruction (NOP ?) ] |
---|
| 488 | ]. |
---|
| 489 | |
---|
[1309] | 490 | definition expand_pseudo_instruction_safe: ? → ? → Word → jump_length → pseudo_instruction → option (list instruction) ≝ |
---|
[825] | 491 | λlookup_labels. |
---|
| 492 | λlookup_datalabels. |
---|
[832] | 493 | λpc. |
---|
| 494 | λjmp_len. |
---|
| 495 | λi. |
---|
| 496 | match i with |
---|
| 497 | [ Cost cost ⇒ Some ? [ ] |
---|
| 498 | | Comment comment ⇒ Some ? [ ] |
---|
| 499 | | Call call ⇒ |
---|
| 500 | match jmp_len with |
---|
| 501 | [ short_jump ⇒ None ? |
---|
| 502 | | medium_jump ⇒ |
---|
| 503 | let 〈ignore, address〉 ≝ split ? 5 11 (lookup_labels call) in |
---|
| 504 | let 〈fst_5, rest〉 ≝ split ? 5 11 pc in |
---|
| 505 | if eq_bv ? ignore fst_5 then |
---|
| 506 | let address ≝ ADDR11 address in |
---|
| 507 | Some ? ([ ACALL address ]) |
---|
| 508 | else |
---|
| 509 | None ? |
---|
| 510 | | long_jump ⇒ |
---|
| 511 | let address ≝ ADDR16 (lookup_labels call) in |
---|
| 512 | Some ? [ LCALL address ] |
---|
| 513 | ] |
---|
| 514 | | Mov d trgt ⇒ |
---|
| 515 | let address ≝ DATA16 (lookup_datalabels trgt) in |
---|
| 516 | Some ? [ RealInstruction (MOV ? (inl ? ? (inl ? ? (inr ? ? 〈DPTR, address〉))))] |
---|
[842] | 517 | | Instruction instr ⇒ expand_relative_jump lookup_labels jmp_len pc instr |
---|
[832] | 518 | | Jmp jmp ⇒ |
---|
| 519 | match jmp_len with |
---|
| 520 | [ short_jump ⇒ |
---|
| 521 | let lookup_address ≝ lookup_labels jmp in |
---|
| 522 | let 〈result, flags〉 ≝ sub_16_with_carry pc lookup_address false in |
---|
| 523 | let 〈upper, lower〉 ≝ split ? 8 8 result in |
---|
| 524 | if eq_bv ? upper (zero 8) then |
---|
| 525 | let address ≝ RELATIVE lower in |
---|
| 526 | Some ? [ SJMP address ] |
---|
| 527 | else |
---|
| 528 | None ? |
---|
| 529 | | medium_jump ⇒ |
---|
| 530 | let address ≝ lookup_labels jmp in |
---|
| 531 | let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 address in |
---|
| 532 | let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in |
---|
| 533 | if eq_bv ? fst_5_addr fst_5_pc then |
---|
| 534 | let address ≝ ADDR11 rest_addr in |
---|
| 535 | Some ? ([ AJMP address ]) |
---|
| 536 | else |
---|
| 537 | None ? |
---|
| 538 | | long_jump ⇒ |
---|
| 539 | let address ≝ ADDR16 (lookup_labels jmp) in |
---|
| 540 | Some ? [ LJMP address ] |
---|
| 541 | ] |
---|
| 542 | ]. |
---|
[825] | 543 | @ I |
---|
| 544 | qed. |
---|
| 545 | |
---|
[1309] | 546 | (* label_trie: identifier ↦ 〈instruction number, address〉 *) |
---|
[1393] | 547 | definition label_trie ≝ BitVectorTrie (nat × nat) 16. |
---|
[1309] | 548 | |
---|
[1393] | 549 | definition add_instruction_size: ℕ → jump_length → pseudo_instruction → ℕ ≝ |
---|
[1309] | 550 | λpc.λjmp_len.λinstr. |
---|
[1393] | 551 | let bv_pc ≝ bitvector_of_nat 16 pc in |
---|
| 552 | let ilist ≝ expand_pseudo_instruction_safe (λx.bv_pc) (λx.bv_pc) bv_pc jmp_len instr in |
---|
[1309] | 553 | let bv: list (BitVector 8) ≝ match ilist with |
---|
| 554 | [ None ⇒ (* hmm, this shouldn't happen *) [ ] |
---|
| 555 | | Some l ⇒ flatten … (map … assembly1 l) |
---|
| 556 | ] in |
---|
[1393] | 557 | pc + (|bv|). |
---|
| 558 | |
---|
[1363] | 559 | definition is_label ≝ |
---|
| 560 | λx:labelled_instruction.λl:Identifier. |
---|
| 561 | let 〈lbl,instr〉 ≝ x in |
---|
| 562 | match lbl with |
---|
| 563 | [ Some l' ⇒ l' = l |
---|
| 564 | | _ ⇒ False |
---|
| 565 | ]. |
---|
[1309] | 566 | |
---|
[1363] | 567 | lemma oeo_Some_Stronger: |
---|
| 568 | ∀id,id',i,prefix. |
---|
| 569 | occurs_exactly_once id (prefix@[〈Some ? id',i〉]) → |
---|
| 570 | (eq_bv ? id' id ∧ does_not_occur id prefix) ∨ |
---|
| 571 | (¬eq_bv ? id' id ∧ occurs_exactly_once id prefix). |
---|
| 572 | #id #id' #i #prefix elim prefix |
---|
| 573 | [ whd in ⊢ (?% → ?) whd in ⊢ (?(match % with [_ ⇒ ?| _ ⇒ ?]) → ?) |
---|
| 574 | change with (? → eq_v ?? eq_b id' id∧?∨?) cases (eq_v ?????) |
---|
| 575 | [ normalize // |
---|
| 576 | | normalize #H @⊥ @H |
---|
| 577 | ] |
---|
| 578 | | *; #he #i' #tl #IH whd in ⊢ (?% → ?) whd in ⊢ (?(match % with [_ ⇒ ? | _ ⇒ ?]) → ?) |
---|
| 579 | cases he; normalize nodelta |
---|
| 580 | [ #H @ (IH H) |
---|
| 581 | | #x whd in ⊢ (? → ?(??%)) whd in match (instruction_matches_identifier ??) |
---|
| 582 | generalize in match (refl ? (eq_bv 16 x id)) change in match (eq_v ???x id) with (eq_bv ? x id) |
---|
| 583 | cases (eq_bv ???) in ⊢ (???% → %) #Heq |
---|
| 584 | [ generalize in match (refl ? (eq_bv 16 id' id)) cases (eq_bv ???) in ⊢ (??%? → %) normalize nodelta |
---|
| 585 | [ #H >(eq_bv_eq … (sym_eq … H)) >does_not_occur_absurd #Hf @⊥ @Hf |
---|
| 586 | | #H >(does_not_occur_Some) |
---|
| 587 | [ #H2 whd in match (does_not_occur ??) whd in match (instruction_matches_identifier ??) |
---|
| 588 | change in match (eq_v ???x id) with (eq_bv ? x id) >Heq normalize nodelta |
---|
| 589 | @orb_elim normalize nodelta whd in match (occurs_exactly_once ??) whd in match (instruction_matches_identifier ??) |
---|
| 590 | change in match (eq_v ???x id) with (eq_bv ? x id) >Heq normalize nodelta @H2 |
---|
| 591 | | @(sym_eq … H) |
---|
| 592 | ] |
---|
| 593 | ] |
---|
| 594 | | normalize nodelta #H lapply (IH H) -IH -H; #Hor @orb_elim |
---|
| 595 | generalize in match (refl ? (eq_bv 16 id' id)) cases (eq_bv ???) in ⊢ (??%? → %) |
---|
| 596 | #Heq2 |
---|
| 597 | [ normalize nodelta <Heq2 in Hor; #Hor normalize in Hor; |
---|
| 598 | whd in match (does_not_occur ??) whd in match (instruction_matches_identifier ??) |
---|
| 599 | change in match (eq_v ???x id) with (eq_bv ? x id) >Heq normalize nodelta |
---|
| 600 | cases (does_not_occur id tl) in Hor; normalize nodelta // |
---|
| 601 | | normalize nodelta whd in match (occurs_exactly_once ??) |
---|
| 602 | whd in match (instruction_matches_identifier ??) change in match (eq_v ???x id) with (eq_bv ? x id) |
---|
| 603 | >Heq normalize nodelta <Heq2 in Hor; normalize // |
---|
| 604 | ] |
---|
| 605 | ] |
---|
| 606 | ] |
---|
| 607 | ] |
---|
| 608 | qed. |
---|
| 609 | |
---|
| 610 | lemma bla: |
---|
| 611 | ∀i,p,l. |
---|
| 612 | is_label (nth i ? p 〈None ?, Comment [ ]〉) l → does_not_occur l p = false. |
---|
| 613 | #i #p #l generalize in match i; elim p |
---|
| 614 | [ #i >nth_nil #H @⊥ @H |
---|
| 615 | | #h #t #IH #i cases i -i |
---|
| 616 | [ cases h #hi #hp cases hi normalize |
---|
| 617 | [ #H @⊥ @H |
---|
| 618 | | #l' #Heq change in match (eq_v ??? l' l) with (eq_bv ? l' l) |
---|
| 619 | >(eq_eq_bv … Heq) // |
---|
| 620 | ] |
---|
| 621 | | #i #H whd in match (does_not_occur ??) |
---|
| 622 | whd in match (instruction_matches_identifier ??) |
---|
| 623 | cases h #hi #hp cases hi normalize |
---|
| 624 | [ @(IH i) @H |
---|
| 625 | | #l' cases (eq_v ??? l' l) |
---|
| 626 | [ normalize // |
---|
| 627 | | normalize @(IH i) @H |
---|
| 628 | ] |
---|
| 629 | ] |
---|
| 630 | ] |
---|
| 631 | ] |
---|
| 632 | qed. |
---|
[1393] | 633 | |
---|
| 634 | lemma coerc_pair_sigma: |
---|
| 635 | ∀A,B,P. ∀p:A × B. P (\snd p) → A × (Σx:B.P x). |
---|
| 636 | #A #B #P * #a #b #p % [@a | /2/] |
---|
| 637 | qed. |
---|
| 638 | coercion coerc_pair_sigma:∀A,B,P. ∀p:A × B. P (\snd p) → A × (Σx:B.P x) |
---|
| 639 | ≝ coerc_pair_sigma on p: (? × ?) to (? × (Sig ??)). |
---|
[1363] | 640 | |
---|
[1393] | 641 | let rec create_label_trie (program: list labelled_instruction) |
---|
| 642 | (policy: jump_expansion_policy): |
---|
| 643 | (Σlabels:label_trie. |
---|
| 644 | ∀i:ℕ.i < |program| → |
---|
| 645 | ∀l.occurs_exactly_once l program → |
---|
| 646 | is_label (nth i ? program 〈None ?, Comment [ ]〉) l → |
---|
| 647 | ∃a.lookup_opt … l labels = Some ? 〈i,a〉 |
---|
| 648 | ) ≝ |
---|
[1363] | 649 | let 〈final_pc, final_labels〉 ≝ |
---|
[1309] | 650 | foldl_strong (option Identifier × pseudo_instruction) |
---|
[1393] | 651 | (λprefix.ℕ × (Σlabels. |
---|
[1363] | 652 | ∀i:ℕ.i < |prefix| → |
---|
[1393] | 653 | ∀l.occurs_exactly_once l prefix → |
---|
[1363] | 654 | is_label (nth i ? prefix 〈None ?, Comment [ ]〉) l → |
---|
[1393] | 655 | ∃a.lookup_opt … l labels = Some ? 〈i,a〉) |
---|
[1363] | 656 | ) |
---|
[1309] | 657 | program |
---|
| 658 | (λprefix.λx.λtl.λprf.λacc. |
---|
[1363] | 659 | let 〈pc,labels〉 ≝ acc in |
---|
[1309] | 660 | let 〈label,instr〉 ≝ x in |
---|
[1393] | 661 | let new_labels ≝ |
---|
| 662 | match label with |
---|
| 663 | [ None ⇒ labels |
---|
| 664 | | Some l ⇒ insert … l 〈|prefix|, pc〉 labels |
---|
| 665 | ] in |
---|
| 666 | let jmp_len ≝ \snd (lookup … (bitvector_of_nat 16 (|prefix|)) policy 〈pc, long_jump〉) in |
---|
| 667 | 〈add_instruction_size pc jmp_len instr, new_labels〉 |
---|
| 668 | ) 〈0, Stub ? ?〉 in |
---|
[1309] | 669 | final_labels. |
---|
[1393] | 670 | [ #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi) -Hi; |
---|
| 671 | [ #Hi #l normalize nodelta; cases label normalize nodelta |
---|
[1363] | 672 | [ >occurs_exactly_once_None #Hocc >(nth_append_first ? ? prefix ? ? (le_S_S_to_le ? ? Hi)) #Hl |
---|
[1393] | 673 | lapply (sig2 … labels) #Hacc elim (Hacc i (le_S_S_to_le … Hi) l Hocc Hl) #addr #Haddr |
---|
[1363] | 674 | % [ @addr | @Haddr ] |
---|
| 675 | | #l' #Hocc #Hl lapply (oeo_Some_Stronger … Hocc) -Hocc; #Hocc |
---|
| 676 | lapply (refl ? (eq_bv 16 l' l)) cases (eq_bv ? l' l) in ⊢ (???% → %) #Heq |
---|
| 677 | [ >Heq in Hocc; #Hocc normalize in Hocc; |
---|
| 678 | >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) in Hl; #Hl |
---|
| 679 | @⊥ @(absurd … Hocc) |
---|
[1393] | 680 | | >Heq in Hocc; normalize nodelta #Hocc lapply (sig2 … labels) #Hacc elim (Hacc i (le_S_S_to_le … Hi) l Hocc ?) |
---|
| 681 | [ #addr #Haddr % [ @addr | <Haddr @lookup_opt_insert_miss >eq_bv_sym >Heq // ] |
---|
| 682 | | >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) in Hl; // |
---|
[1363] | 683 | ] |
---|
| 684 | ] |
---|
| 685 | >(bla i … Hl) normalize nodelta @nmk // |
---|
| 686 | ] |
---|
| 687 | | #Hi #l #Hocc >(injective_S … Hi) >nth_append_second |
---|
[1393] | 688 | [ <minus_n_n #Hl normalize in Hl; normalize nodelta cases label in Hl; |
---|
[1363] | 689 | [ normalize nodelta #H @⊥ @H |
---|
[1393] | 690 | | #l' normalize nodelta #Heq % [ @pc | <Heq normalize nodelta @lookup_opt_insert_hit ] |
---|
[1363] | 691 | ] |
---|
| 692 | | @le_n |
---|
| 693 | ] |
---|
| 694 | ] |
---|
| 695 | | #i #Hi #l #Hl @⊥ @Hl |
---|
| 696 | ] |
---|
[1103] | 697 | qed. |
---|
| 698 | |
---|
[1393] | 699 | definition select_reljump_length: label_trie → ℕ → Identifier → jump_length ≝ |
---|
[1309] | 700 | λlabels.λpc.λlbl. |
---|
| 701 | let 〈n, addr〉 ≝ lookup … lbl labels 〈0, pc〉 in |
---|
[1393] | 702 | if leb pc addr (* forward jump *) |
---|
| 703 | then if leb (addr - pc) 126 |
---|
| 704 | then short_jump |
---|
| 705 | else long_jump |
---|
| 706 | else if leb (pc - addr) 129 |
---|
| 707 | then short_jump |
---|
| 708 | else long_jump. |
---|
[1103] | 709 | |
---|
[1393] | 710 | definition select_call_length: label_trie → ℕ → Identifier → jump_length ≝ |
---|
| 711 | λlabels.λpc_nat.λlbl. |
---|
| 712 | let pc ≝ bitvector_of_nat 16 pc_nat in |
---|
| 713 | let addr ≝ bitvector_of_nat 16 (\snd (lookup ? ? lbl labels 〈0, pc_nat〉)) in |
---|
[1309] | 714 | let 〈fst_5_addr, rest_addr〉 ≝ split ? 5 11 addr in |
---|
| 715 | let 〈fst_5_pc, rest_pc〉 ≝ split ? 5 11 pc in |
---|
| 716 | if eq_bv ? fst_5_addr fst_5_pc |
---|
| 717 | then medium_jump |
---|
| 718 | else long_jump. |
---|
| 719 | |
---|
[1393] | 720 | definition select_jump_length: label_trie → ℕ → Identifier → jump_length ≝ |
---|
[1309] | 721 | λlabels.λpc.λlbl. |
---|
| 722 | let 〈n, addr〉 ≝ lookup … lbl labels 〈0, pc〉 in |
---|
[1393] | 723 | if leb pc addr |
---|
| 724 | then if leb (addr - pc) 126 |
---|
| 725 | then short_jump |
---|
| 726 | else select_call_length labels pc lbl |
---|
| 727 | else if leb (pc - addr) 129 |
---|
| 728 | then short_jump |
---|
| 729 | else select_call_length labels pc lbl. |
---|
[1309] | 730 | |
---|
[1393] | 731 | definition jump_expansion_step_instruction: label_trie → ℕ → |
---|
[1309] | 732 | preinstruction Identifier → option jump_length ≝ |
---|
| 733 | λlabels.λpc.λi. |
---|
| 734 | match i with |
---|
| 735 | [ JC j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 736 | | JNC j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 737 | | JZ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 738 | | JNZ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 739 | | JB _ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 740 | | JBC _ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 741 | | JNB _ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 742 | | CJNE _ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 743 | | DJNZ _ j ⇒ Some ? (select_reljump_length labels pc j) |
---|
| 744 | | _ ⇒ None ? |
---|
| 745 | ]. |
---|
[1103] | 746 | |
---|
[1309] | 747 | definition max_length: jump_length → jump_length → jump_length ≝ |
---|
| 748 | λj1.λj2. |
---|
| 749 | match j1 with |
---|
| 750 | [ long_jump ⇒ long_jump |
---|
| 751 | | medium_jump ⇒ |
---|
| 752 | match j2 with |
---|
| 753 | [ long_jump ⇒ long_jump |
---|
| 754 | | _ ⇒ medium_jump |
---|
| 755 | ] |
---|
| 756 | | short_jump ⇒ j2 |
---|
| 757 | ]. |
---|
[1393] | 758 | |
---|
| 759 | definition jmpleq: jump_length → jump_length → Prop ≝ |
---|
| 760 | λj1.λj2. |
---|
| 761 | match j1 with |
---|
| 762 | [ short_jump ⇒ True |
---|
| 763 | | medium_jump ⇒ |
---|
| 764 | match j2 with |
---|
| 765 | [ short_jump ⇒ False |
---|
| 766 | | _ ⇒ True |
---|
| 767 | ] |
---|
| 768 | | long_jump ⇒ |
---|
| 769 | match j2 with |
---|
| 770 | [ long_jump ⇒ True |
---|
| 771 | | _ ⇒ False |
---|
| 772 | ] |
---|
| 773 | ]. |
---|
| 774 | |
---|
[1417] | 775 | lemma jmpleq_max_length: ∀x,y.jmpleq y (max_length y x). |
---|
| 776 | #x #y |
---|
| 777 | cases y |
---|
| 778 | [ // |
---|
| 779 | | cases x // |
---|
| 780 | | // |
---|
| 781 | ] |
---|
| 782 | qed. |
---|
| 783 | |
---|
[1393] | 784 | definition is_jump' ≝ |
---|
| 785 | λx:preinstruction Identifier. |
---|
| 786 | match x with |
---|
| 787 | [ JC _ ⇒ True |
---|
| 788 | | JNC _ ⇒ True |
---|
| 789 | | JZ _ ⇒ True |
---|
| 790 | | JNZ _ ⇒ True |
---|
| 791 | | JB _ _ ⇒ True |
---|
| 792 | | JNB _ _ ⇒ True |
---|
| 793 | | JBC _ _ ⇒ True |
---|
| 794 | | CJNE _ _ ⇒ True |
---|
| 795 | | DJNZ _ _ ⇒ True |
---|
| 796 | | _ ⇒ False |
---|
| 797 | ]. |
---|
| 798 | |
---|
| 799 | definition is_jump ≝ |
---|
| 800 | λx:labelled_instruction. |
---|
| 801 | let 〈label,instr〉 ≝ x in |
---|
| 802 | match instr with |
---|
| 803 | [ Instruction i ⇒ is_jump' i |
---|
| 804 | | Call _ ⇒ True |
---|
| 805 | | Jmp _ ⇒ True |
---|
| 806 | | _ ⇒ False |
---|
| 807 | ]. |
---|
[1404] | 808 | |
---|
| 809 | definition jump_in_policy ≝ |
---|
| 810 | λprefix:list labelled_instruction.λpolicy:jump_expansion_policy. |
---|
| 811 | ∀i:ℕ.i < |prefix| → is_jump (nth i ? prefix 〈None ?, Comment []〉) → |
---|
| 812 | ∃p,j.lookup_opt … (bitvector_of_nat 16 i) policy = Some ? 〈p,j〉. |
---|
| 813 | |
---|
[1393] | 814 | let rec jump_expansion_step (program: list labelled_instruction) |
---|
[1404] | 815 | (old_policy: Σpolicy.jump_in_policy program policy): |
---|
| 816 | (Σpolicy. |
---|
| 817 | jump_in_policy program policy ∧ |
---|
[1417] | 818 | (jump_in_policy program policy → |
---|
| 819 | (∀i.i < |program| → is_jump (nth i ? program 〈None ?, Comment []〉) → |
---|
| 820 | jmpleq |
---|
| 821 | (\snd (lookup … (bitvector_of_nat 16 i) old_policy 〈0,short_jump〉)) |
---|
| 822 | (\snd (lookup … (bitvector_of_nat 16 i) policy 〈0,short_jump〉))) |
---|
| 823 | ) |
---|
| 824 | ) ≝ |
---|
[1393] | 825 | let labels ≝ create_label_trie program old_policy in |
---|
| 826 | let 〈final_pc, final_policy〉 ≝ |
---|
[1309] | 827 | foldl_strong (option Identifier × pseudo_instruction) |
---|
[1404] | 828 | (λprefix.ℕ × Σpolicy. |
---|
| 829 | jump_in_policy prefix policy ∧ |
---|
[1417] | 830 | (jump_in_policy prefix policy → |
---|
| 831 | (∀i.i < |prefix| → is_jump (nth i ? prefix 〈None ?, Comment []〉) → |
---|
| 832 | jmpleq |
---|
| 833 | (\snd (lookup … (bitvector_of_nat 16 i) old_policy 〈0,short_jump〉)) |
---|
| 834 | (\snd (lookup … (bitvector_of_nat 16 i) policy 〈0,short_jump〉))) |
---|
| 835 | ) |
---|
[1393] | 836 | ) |
---|
[1309] | 837 | program |
---|
| 838 | (λprefix.λx.λtl.λprf.λacc. |
---|
[1393] | 839 | let 〈pc, policy〉 ≝ acc in |
---|
[1309] | 840 | let 〈label,instr〉 ≝ x in |
---|
[1417] | 841 | let old_jump_length ≝ lookup_opt ? ? (bitvector_of_nat 16 (|prefix|)) old_policy in |
---|
[1309] | 842 | let add_instr ≝ |
---|
| 843 | match instr with |
---|
| 844 | [ Instruction i ⇒ jump_expansion_step_instruction labels pc i |
---|
| 845 | | Call c ⇒ Some ? (select_call_length labels pc c) |
---|
| 846 | | Jmp j ⇒ Some ? (select_jump_length labels pc j) |
---|
| 847 | | _ ⇒ None ? |
---|
| 848 | ] in |
---|
| 849 | let 〈new_pc, new_policy〉 ≝ |
---|
[1417] | 850 | let 〈ignore,old_length〉 ≝ lookup … (bitvector_of_nat 16 (|prefix|)) old_policy 〈0, short_jump〉 in |
---|
[1309] | 851 | match add_instr with |
---|
| 852 | [ None ⇒ (* i.e. it's not a jump *) |
---|
| 853 | 〈add_instruction_size pc long_jump instr, policy〉 |
---|
| 854 | | Some ai ⇒ |
---|
| 855 | let new_length ≝ max_length old_length ai in |
---|
[1393] | 856 | 〈add_instruction_size pc new_length instr, insert … (bitvector_of_nat 16 (|prefix|)) 〈pc, new_length〉 policy〉 |
---|
[1309] | 857 | ] in |
---|
[1393] | 858 | 〈new_pc, new_policy〉 |
---|
| 859 | ) 〈0, Stub ? ?〉 in |
---|
[1309] | 860 | final_policy. |
---|
[1404] | 861 | [ @conj |
---|
| 862 | [ #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi) -Hi; |
---|
| 863 | [ #Hi; >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) #Hj |
---|
| 864 | lapply (sig2 … policy) #Hacc elim ((proj1 … Hacc) i (le_S_S_to_le … Hi) Hj) #h #n elim n |
---|
| 865 | -n #n #Hn |
---|
| 866 | % [ @h | % [ @n | normalize nodelta cases instr normalize nodelta |
---|
[1417] | 867 | [2,3: #x cases (lookup ??? old_policy ?) #y #z @Hn |
---|
| 868 | |6: #x #y cases (lookup ??? old_policy ?) #w #z @Hn |
---|
| 869 | |1: #pi cases (lookup ??? old_policy ?) #x #y cases (jump_expansion_step_instruction ???) |
---|
[1404] | 870 | [ @Hn |
---|
| 871 | | #z normalize nodelta <Hn @lookup_opt_insert_miss |
---|
| 872 | @notb_elim >eq_bv_false [ // | @nmk #H @(absurd (i = (|prefix|))) |
---|
| 873 | [ @(bitvector_of_nat_ok … H) |
---|
| 874 | | @(lt_to_not_eq i (|prefix|)) @(le_S_S_to_le … Hi) |
---|
| 875 | ] ] |
---|
[1393] | 876 | ] |
---|
[1417] | 877 | |4,5: #id cases (lookup ??? old_policy ?) #x #y normalize nodelta <Hn |
---|
[1404] | 878 | @lookup_opt_insert_miss @notb_elim >eq_bv_false |
---|
| 879 | [1,3: // |
---|
| 880 | |2,4: @nmk #H @(absurd (i = (|prefix|))) |
---|
| 881 | [1,3: @(bitvector_of_nat_ok … H) |
---|
| 882 | |2,4: @(lt_to_not_eq i (|prefix|)) @(le_S_S_to_le … Hi) |
---|
| 883 | ] ] |
---|
| 884 | ] ] ] |
---|
| 885 | | #Hi >nth_append_second >(injective_S … Hi) |
---|
| 886 | [ <minus_n_n #Hj normalize in Hj; normalize nodelta cases instr in Hj; |
---|
| 887 | [2,3: #x #Hj @⊥ @Hj |
---|
| 888 | |6: #x #y #Hj @⊥ @Hj |
---|
| 889 | |1: #pi cases pi |
---|
| 890 | [35,36,37: #Hj @⊥ @Hj |
---|
| 891 | |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #Hj @⊥ @Hj |
---|
| 892 | |1,2,3,6,7,33,34: #x #y #Hj @⊥ @Hj |
---|
| 893 | |9,10,14,15: #j normalize nodelta #_ |
---|
| 894 | % [1,3,5,7: @pc |
---|
[1417] | 895 | |2,4,6,8: lapply (refl ? (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)) |
---|
| 896 | cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉) in ⊢ (???% → %) |
---|
[1404] | 897 | #x #y #Hl normalize nodelta |
---|
| 898 | % [1,3,5,7: @(max_length y (select_reljump_length (create_label_trie program old_policy) pc j)) |
---|
| 899 | |2,4,6,8: @lookup_opt_insert_hit |
---|
| 900 | ] ] |
---|
| 901 | |11,12,13,16,17: #x #j normalize nodelta #_ |
---|
| 902 | % [1,3,5,7,9: @pc |
---|
[1417] | 903 | |2,4,6,8,10: lapply (refl ? (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉)) |
---|
| 904 | cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉) in ⊢ (???% → %) |
---|
[1404] | 905 | #x #y #Hl normalize nodelta |
---|
| 906 | % [1,3,5,7,9: @(max_length y (select_reljump_length (create_label_trie program old_policy) pc j)) |
---|
| 907 | |2,4,6,8,10: @lookup_opt_insert_hit |
---|
| 908 | ] ] |
---|
| 909 | ] |
---|
| 910 | |4,5: #j normalize nodelta #_ |
---|
| 911 | % [1,3: @pc |
---|
[1417] | 912 | |2,4: cases (lookup … (bitvector_of_nat ? (|prefix|)) old_policy 〈0,short_jump〉) |
---|
[1404] | 913 | #x #y normalize nodelta |
---|
| 914 | % [1: @(max_length y (select_jump_length (create_label_trie program old_policy) pc j)) |
---|
| 915 | |3: @(max_length y (select_call_length (create_label_trie program old_policy) pc j)) |
---|
| 916 | |2,4: @lookup_opt_insert_hit |
---|
| 917 | ] |
---|
[1393] | 918 | ] |
---|
[1404] | 919 | ] |
---|
| 920 | | @le_n |
---|
[1393] | 921 | ] |
---|
| 922 | ] |
---|
[1417] | 923 | | #Hjip #i >append_length <commutative_plus #Hi normalize in Hi; cases (le_to_or_lt_eq … Hi) -Hi; |
---|
| 924 | [ #Hi >(nth_append_first ? ? prefix ? ? (le_S_S_to_le … Hi)) #Hj |
---|
| 925 | lapply (sig2 … policy) #Hpolicy normalize nodelta cases instr normalize nodelta |
---|
| 926 | [2,3: #x cases (lookup ? 16 (bitvector_of_nat 16 (|prefix|)) old_policy ?) #y #z |
---|
| 927 | normalize nodelta @(proj2 ? ? Hpolicy (proj1 ? ? Hpolicy) ? (le_S_S_to_le … Hi) Hj) |
---|
| 928 | |6: #x #y cases (lookup ? 16 (bitvector_of_nat 16 (|prefix|)) old_policy ?) #w #z |
---|
| 929 | normalize nodelta @(proj2 ? ? Hpolicy (proj1 ? ? Hpolicy) ? (le_S_S_to_le … Hi) Hj) |
---|
| 930 | |1: #pi cases (lookup ? 16 (bitvector_of_nat 16 (|prefix|)) old_policy ?) #x #y |
---|
| 931 | cases (jump_expansion_step_instruction ???) |
---|
| 932 | [ @(proj2 ? ? Hpolicy (proj1 ? ? Hpolicy) ? (le_S_S_to_le … Hi) Hj) |
---|
| 933 | | #z normalize nodelta |
---|
| 934 | whd in match (snd ℕ jump_expansion_policy ?) >lookup_insert_miss |
---|
| 935 | [ @(proj2 … Hpolicy (proj1 ? ? Hpolicy) ? (le_S_S_to_le … Hi) Hj) |
---|
| 936 | | @notb_elim >eq_bv_false [ // | @nmk #H @(absurd (i = (|prefix|))) |
---|
| 937 | [ @(bitvector_of_nat_ok … H) |
---|
| 938 | | @(lt_to_not_eq i (|prefix|)) @(le_S_S_to_le … Hi) |
---|
| 939 | ] ] |
---|
| 940 | ] |
---|
| 941 | ] |
---|
| 942 | |4,5: #id cases (lookup ? 16 (bitvector_of_nat 16 (|prefix|)) old_policy ?) #x #y |
---|
| 943 | normalize nodelta >lookup_insert_miss |
---|
| 944 | [1,3: @(proj2 … Hpolicy (proj1 ? ? Hpolicy) ? (le_S_S_to_le … Hi) Hj) |
---|
| 945 | |2,4: @notb_elim >eq_bv_false [1,3: // | 2,4: @nmk #H @(absurd (i = (|prefix|))) |
---|
| 946 | [1,3: @(bitvector_of_nat_ok … H) |
---|
| 947 | |2,4: @(lt_to_not_eq i (|prefix|)) @(le_S_S_to_le … Hi) |
---|
| 948 | ] ] |
---|
| 949 | ] |
---|
| 950 | ] |
---|
| 951 | | #Hi >nth_append_second >(injective_S … Hi) |
---|
| 952 | [ <minus_n_n #Hj normalize in Hj; normalize nodelta cases instr in Hj; |
---|
| 953 | [2,3: #x #Hj @⊥ @Hj |
---|
| 954 | |6: #x #y #Hj @⊥ @Hj |
---|
| 955 | |1: #pi cases pi |
---|
| 956 | [35,36,37: #Hj @⊥ @Hj |
---|
| 957 | |4,5,8,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32: #x #Hj @⊥ @Hj |
---|
| 958 | |1,2,3,6,7,33,34: #x #y #Hj @⊥ @Hj |
---|
| 959 | |9,10,14,15: #j normalize nodelta #_ |
---|
| 960 | cases (lookup ???? 〈0,short_jump〉) #x #y |
---|
| 961 | whd in match (snd ℕ jump_expansion_policy ?) |
---|
| 962 | >lookup_insert_hit normalize @jmpleq_max_length |
---|
| 963 | |11,12,13,16,17: #z #j normalize nodelta #_ |
---|
| 964 | cases (lookup ???? 〈0,short_jump〉) #x #y |
---|
| 965 | whd in match (snd ℕ jump_expansion_policy ?) |
---|
| 966 | >lookup_insert_hit normalize @jmpleq_max_length |
---|
| 967 | ] |
---|
| 968 | |4,5: #id #_ cases (lookup ???? 〈0,short_jump〉) #x #y |
---|
| 969 | whd in match (snd ℕ jump_expansion_policy ?) |
---|
| 970 | >lookup_insert_hit normalize @jmpleq_max_length |
---|
| 971 | ] |
---|
| 972 | | @le_n |
---|
| 973 | ] |
---|
| 974 | ] |
---|
[1393] | 975 | ] |
---|
[1404] | 976 | | @conj |
---|
| 977 | [ #i #Hi @⊥ @(absurd (i<0)) [ @Hi | @(not_le_Sn_O) ] |
---|
[1417] | 978 | | #H #i #Hi @⊥ @(absurd (i<0)) [ @Hi | @(not_le_Sn_O) ] |
---|
[1404] | 979 | ] |
---|
[1393] | 980 | ] |
---|
[1309] | 981 | qed. |
---|
[1103] | 982 | |
---|
[1309] | 983 | definition jump_expansion_internal: list labelled_instruction → jump_expansion_policy ≝ |
---|
| 984 | λprogram. |
---|
| 985 | Stub ? ?. |
---|
| 986 | |
---|
[989] | 987 | (**************************************** START OF POLICY ABSTRACTION ********************) |
---|
| 988 | |
---|
[987] | 989 | definition policy_type ≝ Word → jump_length. |
---|
| 990 | |
---|
| 991 | definition jump_expansion': pseudo_assembly_program → policy_type ≝ |
---|
| 992 | λprogram.λpc. |
---|
[1309] | 993 | let policy ≝ jump_expansion_internal (\snd program) in |
---|
[1393] | 994 | let 〈n,j〉 ≝ lookup ? ? pc policy 〈0, long_jump〉 in |
---|
[1309] | 995 | j. |
---|
| 996 | |
---|
[993] | 997 | definition assembly_1_pseudoinstruction_safe ≝ |
---|
[913] | 998 | λprogram: pseudo_assembly_program. |
---|
[987] | 999 | λjump_expansion: policy_type. |
---|
[922] | 1000 | λppc: Word. |
---|
[913] | 1001 | λpc: Word. |
---|
[832] | 1002 | λlookup_labels. |
---|
| 1003 | λlookup_datalabels. |
---|
| 1004 | λi. |
---|
[987] | 1005 | let expansion ≝ jump_expansion ppc in |
---|
[1309] | 1006 | match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc expansion i with |
---|
[832] | 1007 | [ None ⇒ None ? |
---|
[836] | 1008 | | Some pseudos ⇒ |
---|
| 1009 | let mapped ≝ map ? ? assembly1 pseudos in |
---|
| 1010 | let flattened ≝ flatten ? mapped in |
---|
[922] | 1011 | let pc_len ≝ length ? flattened in |
---|
| 1012 | Some ? (〈pc_len, flattened〉) |
---|
[832] | 1013 | ]. |
---|
[913] | 1014 | |
---|
[993] | 1015 | definition construct_costs_safe ≝ |
---|
[832] | 1016 | λprogram. |
---|
[987] | 1017 | λjump_expansion: policy_type. |
---|
[922] | 1018 | λpseudo_program_counter: nat. |
---|
[832] | 1019 | λprogram_counter: nat. |
---|
| 1020 | λcosts: BitVectorTrie Word 16. |
---|
| 1021 | λi. |
---|
| 1022 | match i with |
---|
[840] | 1023 | [ Cost cost ⇒ |
---|
[832] | 1024 | let program_counter_bv ≝ bitvector_of_nat ? program_counter in |
---|
| 1025 | Some ? 〈program_counter, (insert ? ? program_counter_bv cost costs)〉 |
---|
| 1026 | | _ ⇒ |
---|
| 1027 | let pc_bv ≝ bitvector_of_nat ? program_counter in |
---|
[922] | 1028 | let ppc_bv ≝ bitvector_of_nat ? pseudo_program_counter in |
---|
[993] | 1029 | let lookup_labels ≝ λx.pc_bv in |
---|
| 1030 | let lookup_datalabels ≝ λx.zero ? in |
---|
[836] | 1031 | let pc_delta_assembled ≝ |
---|
[993] | 1032 | assembly_1_pseudoinstruction_safe program jump_expansion ppc_bv pc_bv |
---|
[836] | 1033 | lookup_labels lookup_datalabels i |
---|
| 1034 | in |
---|
| 1035 | match pc_delta_assembled with |
---|
[832] | 1036 | [ None ⇒ None ? |
---|
[836] | 1037 | | Some pc_delta_assembled ⇒ |
---|
| 1038 | let 〈pc_delta, assembled〉 ≝ pc_delta_assembled in |
---|
[837] | 1039 | Some ? 〈pc_delta + program_counter, costs〉 |
---|
[832] | 1040 | ] |
---|
| 1041 | ]. |
---|
| 1042 | |
---|
[985] | 1043 | (* This establishes the correspondence between pseudo program counters and |
---|
| 1044 | program counters. It is at the heart of the proof. *) |
---|
| 1045 | (*CSC: code taken from build_maps *) |
---|
[1309] | 1046 | definition sigma00: pseudo_assembly_program → policy_type → list ? → ? → option (nat × (nat × (BitVectorTrie Word 16))) ≝ |
---|
[987] | 1047 | λinstr_list. |
---|
| 1048 | λjump_expansion: policy_type. |
---|
| 1049 | λl:list labelled_instruction. |
---|
[1309] | 1050 | λacc. |
---|
[985] | 1051 | foldl ?? |
---|
| 1052 | (λt,i. |
---|
[832] | 1053 | match t with |
---|
| 1054 | [ None ⇒ None ? |
---|
[985] | 1055 | | Some ppc_pc_map ⇒ |
---|
| 1056 | let 〈ppc,pc_map〉 ≝ ppc_pc_map in |
---|
| 1057 | let 〈program_counter, sigma_map〉 ≝ pc_map in |
---|
| 1058 | let 〈label, i〉 ≝ i in |
---|
[993] | 1059 | match construct_costs_safe instr_list jump_expansion ppc program_counter (Stub …) i with |
---|
[832] | 1060 | [ None ⇒ None ? |
---|
[985] | 1061 | | Some pc_ignore ⇒ |
---|
| 1062 | let 〈pc,ignore〉 ≝ pc_ignore in |
---|
| 1063 | Some … 〈S ppc,〈pc, insert ? ? (bitvector_of_nat ? ppc) (bitvector_of_nat ? pc) sigma_map〉〉 ] |
---|
[1309] | 1064 | ]) acc l. |
---|
[985] | 1065 | |
---|
[987] | 1066 | definition sigma0: pseudo_assembly_program → policy_type → option (nat × (nat × (BitVectorTrie Word 16))) |
---|
[1309] | 1067 | ≝ λprog.λjump_expansion.sigma00 prog jump_expansion (\snd prog) (Some ? 〈0, 〈0, (Stub ? ?)〉〉). |
---|
[985] | 1068 | |
---|
[987] | 1069 | definition tech_pc_sigma00: pseudo_assembly_program → policy_type → list labelled_instruction → option (nat × nat) ≝ |
---|
| 1070 | λprogram,jump_expansion,instr_list. |
---|
[1309] | 1071 | match sigma00 program jump_expansion instr_list (Some ? 〈0, 〈0, (Stub ? ?)〉〉) (* acc copied from sigma0 *) with |
---|
[985] | 1072 | [ None ⇒ None … |
---|
| 1073 | | Some result ⇒ |
---|
| 1074 | let 〈ppc,pc_sigma_map〉 ≝ result in |
---|
| 1075 | let 〈pc,map〉 ≝ pc_sigma_map in |
---|
| 1076 | Some … 〈ppc,pc〉 ]. |
---|
| 1077 | |
---|
[987] | 1078 | definition sigma_safe: pseudo_assembly_program → policy_type → option (Word → Word) ≝ |
---|
| 1079 | λinstr_list,jump_expansion. |
---|
| 1080 | match sigma0 instr_list jump_expansion with |
---|
[832] | 1081 | [ None ⇒ None ? |
---|
| 1082 | | Some result ⇒ |
---|
[985] | 1083 | let 〈ppc,pc_sigma_map〉 ≝ result in |
---|
| 1084 | let 〈pc, sigma_map〉 ≝ pc_sigma_map in |
---|
[832] | 1085 | if gtb pc (2^16) then |
---|
| 1086 | None ? |
---|
| 1087 | else |
---|
[985] | 1088 | Some ? (λx.lookup ?? x sigma_map (zero …)) ]. |
---|
[832] | 1089 | |
---|
[985] | 1090 | (* stuff about policy *) |
---|
| 1091 | |
---|
[987] | 1092 | definition policy_ok ≝ λjump_expansion,p. sigma_safe p jump_expansion ≠ None …. |
---|
[985] | 1093 | |
---|
[987] | 1094 | definition policy ≝ λp. Σjump_expansion:policy_type. policy_ok jump_expansion p. |
---|
| 1095 | |
---|
| 1096 | lemma eject_policy: ∀p. policy p → policy_type. |
---|
| 1097 | #p #pol @(eject … pol) |
---|
| 1098 | qed. |
---|
| 1099 | |
---|
| 1100 | coercion eject_policy nocomposites: ∀p.∀pol:policy p. policy_type ≝ eject_policy on _pol:(policy ?) to policy_type. |
---|
| 1101 | |
---|
| 1102 | definition sigma: ∀p:pseudo_assembly_program. policy p → Word → Word ≝ |
---|
| 1103 | λp,policy. |
---|
| 1104 | match sigma_safe p (eject … policy) return λr:option (Word → Word). r ≠ None … → Word → Word with |
---|
[985] | 1105 | [ None ⇒ λabs. ⊥ |
---|
[987] | 1106 | | Some r ⇒ λ_.r] (sig2 … policy). |
---|
[993] | 1107 | cases abs /2/ |
---|
[985] | 1108 | qed. |
---|
| 1109 | |
---|
[987] | 1110 | example sigma_0: ∀p,pol. sigma p pol (bitvector_of_nat ? 0) = bitvector_of_nat ? 0. |
---|
[985] | 1111 | cases daemon. |
---|
| 1112 | qed. |
---|
| 1113 | |
---|
[998] | 1114 | definition expand_pseudo_instruction: |
---|
| 1115 | ∀program:pseudo_assembly_program.∀pol: policy program. |
---|
[1014] | 1116 | ∀ppc:Word.∀lookup_labels,lookup_datalabels,pc. |
---|
[998] | 1117 | lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) → |
---|
| 1118 | lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)) → |
---|
[1014] | 1119 | let pi ≝ \fst (fetch_pseudo_instruction (\snd program) ppc) in |
---|
[998] | 1120 | pc = sigma program pol ppc → |
---|
| 1121 | Σres:list instruction. Some … res = expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) pi |
---|
[1014] | 1122 | ≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pc,prf1,prf2,prf3. |
---|
| 1123 | match expand_pseudo_instruction_safe lookup_labels lookup_datalabels pc (pol ppc) (\fst (fetch_pseudo_instruction (\snd program) ppc)) with |
---|
[998] | 1124 | [ None ⇒ let dummy ≝ [ ] in dummy |
---|
| 1125 | | Some res ⇒ res ]. |
---|
| 1126 | [ cases daemon |
---|
| 1127 | | >p %] |
---|
| 1128 | qed. |
---|
| 1129 | |
---|
[993] | 1130 | (* MAIN AXIOM HERE, HIDDEN USING cases daemon *) |
---|
[1309] | 1131 | definition assembly_1_pseudoinstruction': |
---|
[993] | 1132 | ∀program:pseudo_assembly_program.∀pol: policy program. |
---|
| 1133 | ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi. |
---|
| 1134 | lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) → |
---|
| 1135 | lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)) → |
---|
| 1136 | \fst (fetch_pseudo_instruction (\snd program) ppc) = pi → |
---|
| 1137 | Σres:(nat × (list Byte)). |
---|
[1309] | 1138 | Some … res = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi ∧ |
---|
[993] | 1139 | let 〈len,code〉 ≝ res in |
---|
| 1140 | sigma program pol (\snd (half_add ? ppc (bitvector_of_nat ? 1))) = |
---|
| 1141 | bitvector_of_nat … (nat_of_bitvector … (sigma program pol ppc) + len) |
---|
| 1142 | ≝ λprogram: pseudo_assembly_program. |
---|
| 1143 | λpol: policy program. |
---|
| 1144 | λppc: Word. |
---|
| 1145 | λlookup_labels. |
---|
| 1146 | λlookup_datalabels. |
---|
| 1147 | λpi. |
---|
| 1148 | λprf1,prf2,prf3. |
---|
| 1149 | match assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi with |
---|
| 1150 | [ None ⇒ let dummy ≝ 〈0,[ ]〉 in dummy |
---|
| 1151 | | Some res ⇒ res ]. |
---|
| 1152 | [ @⊥ elim pi in p [*] |
---|
| 1153 | try (#ARG1 #ARG2 #ARG3 #abs) try (#ARG1 #ARG2 #abs) try (#ARG1 #abs) try #abs |
---|
| 1154 | generalize in match (jmeq_to_eq ??? abs) -abs; #abs whd in abs:(??%?); try destruct(abs) |
---|
| 1155 | whd in abs:(??match % with [_ ⇒ ? | _ ⇒ ?]?); |
---|
| 1156 | (* WRONG HERE, NEEDS LEMMA SAYING THAT THE POLICY DOES NOT RETURN MEDIUM! *) |
---|
| 1157 | cases daemon |
---|
[1309] | 1158 | | % [ >p %] |
---|
| 1159 | cases res in p ⊢ %; -res; #len #code #EQ normalize nodelta; |
---|
[993] | 1160 | (* THIS SHOULD BE TRUE INSTEAD *) |
---|
| 1161 | cases daemon] |
---|
| 1162 | qed. |
---|
| 1163 | |
---|
[1309] | 1164 | definition assembly_1_pseudoinstruction: |
---|
| 1165 | ∀program:pseudo_assembly_program.∀pol: policy program. |
---|
| 1166 | ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi. |
---|
| 1167 | lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)) → |
---|
| 1168 | lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)) → |
---|
| 1169 | \fst (fetch_pseudo_instruction (\snd program) ppc) = pi → |
---|
| 1170 | nat × (list Byte) |
---|
| 1171 | ≝ λprogram,pol,ppc,lookup_labels,lookup_datalabels,pi,prf1,prf2,prf3. |
---|
| 1172 | assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1 |
---|
| 1173 | prf2 prf3. |
---|
| 1174 | |
---|
| 1175 | lemma assembly_1_pseudoinstruction_ok1: |
---|
| 1176 | ∀program:pseudo_assembly_program.∀pol: policy program. |
---|
| 1177 | ∀ppc:Word.∀lookup_labels,lookup_datalabels,pi. |
---|
| 1178 | ∀prf1:lookup_labels = (λx. sigma program pol (address_of_word_labels_code_mem (\snd program) x)). |
---|
| 1179 | ∀prf2:lookup_datalabels = (λx. lookup ?? x (construct_datalabels (\fst program)) (zero ?)). |
---|
| 1180 | ∀prf3:\fst (fetch_pseudo_instruction (\snd program) ppc) = pi. |
---|
| 1181 | Some … (assembly_1_pseudoinstruction program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3) |
---|
| 1182 | = assembly_1_pseudoinstruction_safe program pol ppc (sigma program pol ppc) lookup_labels lookup_datalabels pi. |
---|
| 1183 | #program #pol #ppc #lookup_labels #lookup_datalabels #pi #prf1 #prf2 #prf3 |
---|
| 1184 | cases (sig2 … (assembly_1_pseudoinstruction' program pol ppc lookup_labels lookup_datalabels pi prf1 prf2 prf3)) |
---|
| 1185 | #H1 #_ @H1 |
---|
| 1186 | qed. |
---|
| 1187 | |
---|
[993] | 1188 | (* MAIN AXIOM HERE, HIDDEN USING cases daemon *) |
---|
| 1189 | definition construct_costs': |
---|
[1309] | 1190 | ∀program. ∀pol:policy program. ∀ppc,pc,costs,i. |
---|
| 1191 | Σres:(nat × (BitVectorTrie Word 16)). Some … res = construct_costs_safe program pol ppc pc costs i |
---|
[993] | 1192 | ≝ |
---|
| 1193 | λprogram.λpol: policy program.λppc,pc,costs,i. |
---|
| 1194 | match construct_costs_safe program pol ppc pc costs i with |
---|
| 1195 | [ None ⇒ let dummy ≝ 〈0, Stub ??〉 in dummy |
---|
| 1196 | | Some res ⇒ res ]. |
---|
| 1197 | [ cases daemon |
---|
[1309] | 1198 | | >p %] |
---|
[993] | 1199 | qed. |
---|
| 1200 | |
---|
| 1201 | definition construct_costs ≝ |
---|
| 1202 | λprogram,pol,ppc,pc,costs,i. eject … (construct_costs' program pol ppc pc costs i). |
---|
| 1203 | |
---|
[1309] | 1204 | lemma option_destruct_Some: ∀A,a,b. Some A a = Some A b → a=b. |
---|
| 1205 | #A #a #b #EQ destruct // |
---|
| 1206 | qed. |
---|
| 1207 | |
---|
| 1208 | lemma sigma00_strict: |
---|
| 1209 | ∀instr_list,jump_expansion,l,acc. acc = None ? → |
---|
| 1210 | sigma00 instr_list jump_expansion l acc = None …. |
---|
| 1211 | #instr_list #jump_expansion #l elim l |
---|
| 1212 | [ #acc #H >H % |
---|
| 1213 | | #hd #tl #IH #acc #H >H change with (sigma00 ?? tl ? = ?) @IH % ] |
---|
| 1214 | qed. |
---|
| 1215 | |
---|
| 1216 | lemma sigma00_append: |
---|
| 1217 | ∀instr_list,jump_expansion,l1,l2,acc. |
---|
| 1218 | sigma00 instr_list jump_expansion (l1@l2) acc = |
---|
| 1219 | sigma00 instr_list jump_expansion |
---|
| 1220 | l2 (sigma00 instr_list jump_expansion l1 acc). |
---|
| 1221 | whd in match sigma00; normalize nodelta; |
---|
| 1222 | #instr_list #jump_expansion #l1 #l2 #acc @foldl_append |
---|
| 1223 | qed. |
---|
| 1224 | |
---|
| 1225 | lemma policy_ok_prefix_ok: |
---|
| 1226 | ∀program.∀pol:policy program.∀suffix,prefix. |
---|
| 1227 | prefix@suffix = \snd program → |
---|
| 1228 | sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) ≠ None …. |
---|
| 1229 | * #preamble #instr_list #pol #suffix #prefix #prf whd in prf:(???%); |
---|
| 1230 | generalize in match (sig2 ?? pol) whd in prf:(???%) <prf in pol; #pol |
---|
| 1231 | whd in match policy_ok; whd in match sigma_safe; whd in match sigma0 |
---|
| 1232 | normalize nodelta >sigma00_append |
---|
| 1233 | cases (sigma00 ?? prefix ?) |
---|
| 1234 | [2: #x #_ % #abs destruct(abs) |
---|
| 1235 | | * #abs @⊥ @abs >sigma00_strict % ] |
---|
| 1236 | qed. |
---|
| 1237 | |
---|
| 1238 | lemma policy_ok_prefix_hd_ok: |
---|
| 1239 | ∀program.∀pol:policy program.∀suffix,hd,prefix,ppc_pc_map. |
---|
| 1240 | (prefix@[hd])@suffix = \snd program → |
---|
| 1241 | Some ? ppc_pc_map = sigma00 program pol prefix (Some … 〈0, 〈0, Stub …〉〉) → |
---|
| 1242 | let 〈ppc,pc_map〉 ≝ ppc_pc_map in |
---|
| 1243 | let 〈program_counter, sigma_map〉 ≝ pc_map in |
---|
| 1244 | let 〈label, i〉 ≝ hd in |
---|
| 1245 | construct_costs_safe program pol ppc program_counter (Stub …) i ≠ None …. |
---|
| 1246 | * #preamble #instr_list #pol #suffix #hd #prefix #ppc_pc_map #EQ1 #EQ2 |
---|
| 1247 | generalize in match (policy_ok_prefix_ok 〈preamble,instr_list〉 pol suffix |
---|
| 1248 | (prefix@[hd]) EQ1) in ⊢ ? >sigma00_append <EQ2 whd in ⊢ (?(??%?) → ?) |
---|
| 1249 | @pair_elim' #ppc #pc_map #EQ3 normalize nodelta |
---|
| 1250 | @pair_elim' #pc #map #EQ4 normalize nodelta |
---|
| 1251 | @pair_elim' #l' #i' #EQ5 normalize nodelta |
---|
| 1252 | cases (construct_costs_safe ??????) normalize |
---|
| 1253 | [* #abs @⊥ @abs % | #X #_ % #abs destruct(abs)] |
---|
| 1254 | qed. |
---|
| 1255 | |
---|
| 1256 | (* |
---|
| 1257 | lemma tech_pc_sigma00_append_Some: |
---|
| 1258 | ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc. |
---|
| 1259 | tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 → |
---|
| 1260 | tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉. |
---|
| 1261 | #program #pol #prefix #costs #label #i #ppc #pc #H |
---|
| 1262 | whd in match tech_pc_sigma00 in ⊢ %; normalize nodelta; |
---|
| 1263 | whd in match sigma00 in ⊢ %; normalize nodelta in ⊢ %; |
---|
| 1264 | generalize in match (sig2 … pol) whd in ⊢ (% → ?) whd in ⊢ (?(??%?) → ?) |
---|
| 1265 | whd in match sigma0; normalize nodelta; |
---|
| 1266 | >foldl_step |
---|
| 1267 | change with (? → match match sigma00 program pol prefix with [None ⇒ ? | Some res ⇒ ?] with [ None ⇒ ? | Some res ⇒ ? ] = ?) |
---|
| 1268 | whd in match tech_pc_sigma00 in H; normalize nodelta in H; |
---|
| 1269 | cases (sigma00 program pol prefix) in H ⊢ % |
---|
| 1270 | [ whd in ⊢ (??%% → ?) #abs destruct(abs) |
---|
| 1271 | | * #ppc' * #pc' #sigma_map normalize nodelta; #H generalize in match (option_destruct_Some ??? H) |
---|
| 1272 | |
---|
| 1273 | normalize nodelta; -H; |
---|
| 1274 | |
---|
| 1275 | |
---|
| 1276 | generalize in match H; -H; |
---|
| 1277 | generalize in match (foldl ?????); in H ⊢ (??match match % with [_ ⇒ ? | _ ⇒ ?] with [_ ⇒ ? | _ ⇒ ?]?) |
---|
| 1278 | [2: whd in ⊢ (??%%) |
---|
| 1279 | XXX |
---|
| 1280 | *) |
---|
| 1281 | |
---|
[985] | 1282 | axiom construct_costs_sigma: |
---|
[993] | 1283 | ∀p.∀pol:policy p.∀ppc,pc,costs,i. |
---|
[987] | 1284 | bitvector_of_nat ? pc = sigma p pol (bitvector_of_nat ? ppc) → |
---|
[993] | 1285 | bitvector_of_nat ? (\fst (construct_costs p pol ppc pc costs i)) = sigma p pol (bitvector_of_nat 16 (S ppc)). |
---|
[985] | 1286 | |
---|
| 1287 | axiom tech_pc_sigma00_append_Some: |
---|
[993] | 1288 | ∀program.∀pol:policy program.∀prefix,costs,label,i,ppc,pc. |
---|
[987] | 1289 | tech_pc_sigma00 program pol prefix = Some … 〈ppc,pc〉 → |
---|
[993] | 1290 | tech_pc_sigma00 program pol (prefix@[〈label,i〉]) = Some … 〈S ppc,\fst (construct_costs program pol … ppc pc costs i)〉. |
---|
[985] | 1291 | |
---|
[989] | 1292 | definition build_maps: |
---|
| 1293 | ∀pseudo_program.∀pol:policy pseudo_program. |
---|
| 1294 | Σres:((BitVectorTrie Word 16) × (BitVectorTrie Word 16)). |
---|
| 1295 | let 〈labels,costs〉 ≝ res in |
---|
| 1296 | ∀id. occurs_exactly_once id (\snd pseudo_program) → |
---|
| 1297 | lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem (\snd pseudo_program) id) |
---|
[993] | 1298 | ≝ |
---|
[987] | 1299 | λpseudo_program.λpol:policy pseudo_program. |
---|
[985] | 1300 | let result ≝ |
---|
| 1301 | foldl_strong |
---|
| 1302 | (option Identifier × pseudo_instruction) |
---|
| 1303 | (λpre. Σres:((BitVectorTrie Word 16) × (nat × (nat × (BitVectorTrie Word 16)))). |
---|
| 1304 | let 〈labels,ppc_pc_costs〉 ≝ res in |
---|
| 1305 | let 〈ppc',pc_costs〉 ≝ ppc_pc_costs in |
---|
| 1306 | let 〈pc',costs〉 ≝ pc_costs in |
---|
[993] | 1307 | And ( And ( |
---|
| 1308 | And (bitvector_of_nat ? pc' = sigma pseudo_program pol (bitvector_of_nat ? ppc')) (*∧*) |
---|
| 1309 | (ppc' = length … pre)) (*∧ *) |
---|
| 1310 | (tech_pc_sigma00 pseudo_program (eject … pol) pre = Some ? 〈ppc',pc'〉)) (*∧*) |
---|
| 1311 | (∀id. occurs_exactly_once id pre → |
---|
| 1312 | lookup ?? id labels (zero …) = sigma pseudo_program pol (address_of_word_labels_code_mem pre id))) |
---|
[985] | 1313 | (\snd pseudo_program) |
---|
| 1314 | (λprefix,i,tl,prf,t. |
---|
| 1315 | let 〈labels, ppc_pc_costs〉 ≝ t in |
---|
| 1316 | let 〈ppc, pc_costs〉 ≝ ppc_pc_costs in |
---|
| 1317 | let 〈pc,costs〉 ≝ pc_costs in |
---|
| 1318 | let 〈label, i'〉 ≝ i in |
---|
| 1319 | let labels ≝ |
---|
| 1320 | match label with |
---|
| 1321 | [ None ⇒ labels |
---|
| 1322 | | Some label ⇒ |
---|
| 1323 | let program_counter_bv ≝ bitvector_of_nat ? pc in |
---|
[993] | 1324 | insert ? ? label program_counter_bv labels] |
---|
[985] | 1325 | in |
---|
[993] | 1326 | let construct ≝ construct_costs pseudo_program pol ppc pc costs i' in |
---|
| 1327 | 〈labels, 〈S ppc,construct〉〉 |
---|
[985] | 1328 | ) 〈(Stub ? ?), 〈0, 〈0, Stub ? ?〉〉〉 |
---|
| 1329 | in |
---|
| 1330 | let 〈labels, ppc_pc_costs〉 ≝ result in |
---|
| 1331 | let 〈ppc,pc_costs〉 ≝ ppc_pc_costs in |
---|
| 1332 | let 〈pc, costs〉 ≝ pc_costs in |
---|
| 1333 | 〈labels, costs〉. |
---|
[993] | 1334 | [2: whd generalize in match (sig2 … t) >p >p1 >p2 >p3 * * * #IHn1 #IH0 #IH1 #IH2 |
---|
| 1335 | generalize in match (refl … construct); cases construct in ⊢ (???% → %) #PC #CODE #JMEQ % [% [%]] |
---|
| 1336 | [ <(construct_costs_sigma … costs i1 IHn1) change with (? = ?(\fst construct)) >JMEQ % |
---|
[1363] | 1337 | | >append_length <IH0 normalize; -IHn1; (*CSC: otherwise it diverges!*) // |
---|
[993] | 1338 | | >(tech_pc_sigma00_append_Some … costs … IH1) change with (Some … 〈S ppc,\fst construct〉 = ?) >JMEQ % |
---|
[985] | 1339 | | #id normalize nodelta; -labels1; cases label; normalize nodelta |
---|
| 1340 | [ #K <address_of_word_labels_code_mem_None [2:@K] @IH2 -IHn1; (*CSC: otherwise it diverges!*) // |
---|
| 1341 | | #l #H generalize in match (occurs_exactly_once_Some ???? H) in ⊢ ?; |
---|
| 1342 | generalize in match (refl … (eq_bv ? l id)); cases (eq_bv … l id) in ⊢ (???% → %) |
---|
| 1343 | [ #EQ #_ <(eq_bv_eq … EQ) >lookup_insert_hit >address_of_word_labels_code_mem_Some_hit |
---|
| 1344 | <IH0 [@IHn1 | <(eq_bv_eq … EQ) in H #H @H] |
---|
| 1345 | | #EQ change with (occurs_exactly_once ?? → ?) #K >lookup_insert_miss [2: -IHn1; /2/] |
---|
| 1346 | <(address_of_word_labels_code_mem_Some_miss … EQ … H) @IH2 -IHn1; //]]] |
---|
[993] | 1347 | |3: whd % [% [%]] // [>sigma_0//] #id normalize in ⊢ (% → ?) #abs @⊥ // |
---|
| 1348 | | generalize in match (sig2 … result) in ⊢ ?; |
---|
| 1349 | normalize nodelta in p ⊢ %; -result; >p in ⊢ (match % with [_ ⇒ ?] → ?) |
---|
| 1350 | normalize nodelta; >p1 normalize nodelta; >p2; normalize nodelta; * #_; #H @H] |
---|
[985] | 1351 | qed. |
---|
| 1352 | |
---|
[993] | 1353 | definition assembly: |
---|
| 1354 | ∀p:pseudo_assembly_program. policy p → list Byte × (BitVectorTrie Identifier 16) ≝ |
---|
| 1355 | λp.let 〈preamble, instr_list〉 ≝ p in |
---|
| 1356 | λpol. |
---|
| 1357 | let 〈labels,costs〉 ≝ build_maps 〈preamble,instr_list〉 pol in |
---|
[985] | 1358 | let datalabels ≝ construct_datalabels preamble in |
---|
| 1359 | let lookup_labels ≝ λx. lookup ? ? x labels (zero ?) in |
---|
| 1360 | let lookup_datalabels ≝ λx. lookup ? ? x datalabels (zero ?) in |
---|
[993] | 1361 | let result ≝ |
---|
| 1362 | foldl_strong |
---|
| 1363 | (option Identifier × pseudo_instruction) |
---|
| 1364 | (λpre. Σpc_ppc_code:(Word × (Word × (list Byte))). |
---|
| 1365 | let 〈pc,ppc_code〉 ≝ pc_ppc_code in |
---|
| 1366 | let 〈ppc,code〉 ≝ ppc_code in |
---|
| 1367 | ∀ppc'. |
---|
| 1368 | let 〈pi,newppc〉 ≝ fetch_pseudo_instruction instr_list ppc' in |
---|
| 1369 | let 〈len,assembledi〉 ≝ |
---|
| 1370 | assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc' lookup_labels lookup_datalabels pi ??? in |
---|
| 1371 | True) |
---|
| 1372 | instr_list |
---|
| 1373 | (λprefix,hd,tl,prf,pc_ppc_code. |
---|
| 1374 | let 〈pc, ppc_code〉 ≝ pc_ppc_code in |
---|
| 1375 | let 〈ppc, code〉 ≝ ppc_code in |
---|
| 1376 | let 〈pc_delta, program〉 ≝ assembly_1_pseudoinstruction 〈preamble,instr_list〉 pol ppc lookup_labels lookup_datalabels (\snd hd) ??? in |
---|
| 1377 | let 〈new_pc, flags〉 ≝ add_16_with_carry pc (bitvector_of_nat ? pc_delta) false in |
---|
| 1378 | let new_ppc ≝ \snd (half_add ? ppc (bitvector_of_nat ? 1)) in |
---|
| 1379 | 〈new_pc, 〈new_ppc, (code @ program)〉〉) |
---|
| 1380 | 〈(zero ?), 〈(zero ?), [ ]〉〉 |
---|
[985] | 1381 | in |
---|
[993] | 1382 | 〈\snd (\snd result), costs〉. |
---|
| 1383 | [2,5: % |
---|
| 1384 | |*: cases daemon ] |
---|
| 1385 | qed. |
---|
[832] | 1386 | |
---|
[820] | 1387 | definition assembly_unlabelled_program: assembly_program → option (list Byte × (BitVectorTrie Identifier 16)) ≝ |
---|
[1014] | 1388 | λp. Some ? (〈foldr ? ? (λi,l. assembly1 i @ l) [ ] p, Stub …〉). |
---|