File size: 15,581 Bytes
9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 0e9783b 9ad95e5 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 | import CM.SimMemOps
/-!
# CM.SimDiv
`select`, `cmp`, `div`, `sdiv`: the instructions whose lowering branches inside the instruction.
`select` skips a `mov` with `beqz` (`cmp r1, #0` and a branch); `cmp` sets `1`, then skips the `0` with `bcc` on the IR's own
condition. The divisions guard the zero divisor with `bnez`; after it Thumb-2 `udiv`/`sdiv` (the model's `divu`/`div`) are
`BitVec.udiv`/`BitVec.sdiv` exactly (`div` of the most negative number by `-1` wraps), so the
division is one instruction.
-/
namespace WordDialect
namespace CM
open Reg
section Div
variable {c : Cfg} {p : Prog 32} {s : State 32} {x : M} {code : List (Instr Nat)}
{L : Layout} {base : Nat} {off : Nat → Nat} {g : Facts}
/-- `Rel0` ignores scratch registers. -/
theorem rel0_scratch (hs : Rel0 c p s x) {x' : M} (hm : x'.mem = x.mem)
(hr : ∀ r, r ≠ r0 → r ≠ r1 → r ≠ r2 → r ≠ r3 → x'.regs r = x.regs r) : Rel0 c p s x' :=
hs.congr hm (hr _ (by decide) (by decide) (by decide) (by decide))
(hr _ (by decide) (by decide) (by decide) (by decide))
(hr _ (by decide) (by decide) (by decide) (by decide))
(hr _ (by decide) (by decide) (by decide) (by decide))
(hr _ (by decide) (by decide) (by decide) (by decide))
(hr _ (by decide) (by decide) (by decide) (by decide))
theorem zg_pass (hs : Rel0 c p s x) {b : W} (hb : x.regs r1 = b) (hne : b ≠ 0#32) {t : Nat}
(ht : t = x.pc + 2) (hat : RAt code x.pc [.bnez r1 t, .exitTrap .divideByZero]) :
∃ x', RSteps code x x' ∧ Rel0 c p s x' ∧ x'.pc = x.pc + 2 ∧ x'.regs = x.regs ∧
x'.mem = x.mem := by
obtain ⟨f0, _⟩ := hat
have hst : step code x = .next { x with pc := t } := by
rw [rstep_of_fetch f0]; simp [exec, hb, hne]
exact ⟨{ x with pc := t }, RSteps.single hst, hs.congr rfl rfl rfl rfl rfl rfl rfl, ht, rfl, rfl⟩
theorem zg_trap (hb : x.regs r1 = 0#32) {t : Nat}
(hat : RAt code x.pc [.bnez r1 t, .exitTrap .divideByZero]) :
RExec code x (.trapped .divideByZero) := by
obtain ⟨f0, f1, _⟩ := hat
refine .next (m1 := x.adv) ?_ (.trap ?_)
· rw [rstep_of_fetch f0]; simp [exec, hb]
· rw [rstep_of_fetch (m := x.adv) (by simpa [M.adv] using f1)]; simp [exec]
/-- `div` and `sdiv` share their lowering shape: load both operands, trap on a zero divisor,
divide into `r0`, store and pop. `g` is the Thumb-2 division (defined at zero), `f` the IR's. -/
theorem sim_divop_ok (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x)
(hpc : x.pc = base) (i : Instr Nat) (gf f : W → W → W)
(hi : ∀ m : M, exec i m = .next (m.arith r0 (gf (m.regs r0) (m.regs r1))))
(hfg : ∀ a b, b ≠ 0#32 → gf a b = f a b)
(hat : RAt code base (uf L (need g 2) base ++ [.load r1 Reg.r4 0, .load r0 Reg.r4 4,
.bnez r1 (base + ufSize (need g 2) + 4), .exitTrap .divideByZero, i, .store Reg.r4 4 r0,
.addImm Reg.r4 4]))
{a b : W} {d : List W} (hd : s.dstack = b :: a :: d) (hb : b ≠ 0#32) :
∃ x', RSteps code x x' ∧ Rel0 c p { s with dstack := f a b :: d } x' ∧
x'.pc = base + (ufSize (need g 2) + 7) := by
obtain ⟨hatuf, hatr⟩ := rat_append.mp hat
have hufl := uf_length L (need g 2) base
rw [hufl] at hatr
obtain ⟨f0, f1, f2, f3, f4, f5, f6, _⟩ := hatr
have hk : 2 ≤ s.dstack.length := by simp [hd]
obtain ⟨x1, hs1, hr1, hpc1, hm1, _⟩ := uf_pass hg hs hL (by decide) hk hpc hatuf
obtain ⟨x2, hs2, hr2, hpc2, hv2, ho2, _⟩ := ld_step (code := code) hr1 (j := 0) (v := b)
(by simp [hd]) (rd := r1) (by simp) (disp := 0) (by simp) (fetch_cast f0 (by omega))
obtain ⟨x3, hs3, hr3, hpc3, hv3, ho3, _⟩ := ld_step (code := code) hr2 (j := 1) (v := a)
(by simp [hd]) (rd := r0) (by simp) (disp := 4) (by simp) (fetch_cast f1 (by omega))
have hcx3 : x3.regs r1 = b := by rw [ho3 r1 (by decide), hv2]
obtain ⟨x4, hs4, hr4, hpc4, hreg4, hm4⟩ := zg_pass (code := code) hr3 hcx3 hb
(t := base + ufSize (need g 2) + 4) (by omega)
⟨fetch_cast f2 (by omega), fetch_cast f3 (by omega), trivial⟩
have hax4 : x4.regs r0 = a := by rw [hreg4, hv3]
have hcx4 : x4.regs r1 = b := by rw [hreg4, hcx3]
let x5 := x4.arith r0 (gf (x4.regs r0) (x4.regs r1))
have hst5 : step code x4 = .next x5 := by rw [rstep_of_fetch (fetch_cast f4 (by omega)), hi]
have hr5 : Rel0 c p s x5 := arith_rel0 hr4 r0 (scratch_ne r0 (by simp)) _
have hax5 : x5.regs r0 = f a b := by simp [x5, M.arith, M.setReg, hax4, hcx4, hfg a b hb]
obtain ⟨x6, hs6, hr6, hpc6, _⟩ := st_step (code := code) hg hr5 (j := 1) (by simp [hd])
(rs := r0) (disp := 4) (by simp) (fetch_cast f5 (by simp [x5, M.arith]; omega))
have hlen : 1 ≤ ({ s with dstack := s.dstack.set 1 (x5.regs r0) } : State 32).dstack.length := by
simp [hd]
obtain ⟨x7, hs7, hr7, hpc7, _⟩ := adj_step (code := code) hg hr6 (n := 1) hlen (δ := 4) (by simp)
(fetch_cast f6 (by simp [x5, M.arith] at hpc6 ⊢; omega))
have hr7' : Rel0 c p { s with dstack := (s.dstack.set 1 (x5.regs r0)).drop 1 } x7 := hr7
have e : (s.dstack.set 1 (x5.regs r0)).drop 1 = f a b :: d := by simp [hd, hax5]
rw [e] at hr7'
exact ⟨x7, ((((hs1.trans hs2).trans hs3).trans hs4).trans
((RSteps.single hst5).trans (hs6.trans hs7))), hr7', by
simp [x5, M.arith] at hpc7 hpc6 ⊢; omega⟩
theorem sim_divop_zero (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x)
(hpc : x.pc = base) (rest : List (Instr Nat))
(hat : RAt code base (uf L (need g 2) base ++ ([.load r1 Reg.r4 0, .load r0 Reg.r4 4,
.bnez r1 (base + ufSize (need g 2) + 4), .exitTrap .divideByZero] ++ rest)))
{a : W} {d : List W} (hd : s.dstack = 0#32 :: a :: d) : RExec code x (.trapped .divideByZero) := by
obtain ⟨hatuf, hatr⟩ := rat_append.mp hat
have hufl := uf_length L (need g 2) base
rw [hufl] at hatr
obtain ⟨f0, f1, f2, f3, _⟩ := hatr
have hk : 2 ≤ s.dstack.length := by simp [hd]
obtain ⟨x1, hs1, hr1, hpc1, hm1, _⟩ := uf_pass hg hs hL (by decide) hk hpc hatuf
obtain ⟨x2, hs2, hr2, hpc2, hv2, ho2, _⟩ := ld_step (code := code) hr1 (j := 0) (v := 0#32)
(by simp [hd]) (rd := r1) (by simp) (disp := 0) (by simp) (fetch_cast f0 (by omega))
obtain ⟨x3, hs3, hr3, hpc3, hv3, ho3, _⟩ := ld_step (code := code) hr2 (j := 1) (v := a)
(by simp [hd]) (rd := r0) (by simp) (disp := 4) (by simp) (fetch_cast f1 (by omega))
have hcx3 : x3.regs r1 = 0#32 := by rw [ho3 r1 (by decide), hv2]
exact RExec.of_steps ((hs1.trans hs2).trans hs3) (zg_trap (code := code) hcx3
⟨fetch_cast f2 (by omega), fetch_cast f3 (by omega), trivial⟩)
theorem sim_div_ok (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x) (hpc : x.pc = base)
(hat : RAt code base (lowerInstr L base off g .div)) {a b : W} {d : List W}
(hd : s.dstack = b :: a :: d) (hb : b ≠ 0#32) :
∃ x', RSteps code x x' ∧ Rel0 c p { s with dstack := a.udiv b :: d } x' ∧
x'.pc = base + isize g (.div : WordDialect.Instr 32) :=
sim_divop_ok hg hL hs hpc (.divu r0 r0 r1) divu BitVec.udiv (fun _ => rfl)
(fun a b h => by simp [divu, h]) hat hd hb
theorem sim_div_zero (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x)
(hpc : x.pc = base) (hat : RAt code base (lowerInstr L base off g .div)) {a : W} {d : List W}
(hd : s.dstack = 0#32 :: a :: d) : RExec code x (.trapped .divideByZero) :=
sim_divop_zero hg hL hs hpc [.divu r0 r0 r1, .store Reg.r4 4 r0, .addImm Reg.r4 4] hat hd
theorem sim_sdiv_ok (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x) (hpc : x.pc = base)
(hat : RAt code base (lowerInstr L base off g .sdiv)) {a b : W} {d : List W}
(hd : s.dstack = b :: a :: d) (hb : b ≠ 0#32) :
∃ x', RSteps code x x' ∧ Rel0 c p { s with dstack := a.sdiv b :: d } x' ∧
x'.pc = base + isize g (.sdiv : WordDialect.Instr 32) :=
sim_divop_ok hg hL hs hpc (.div r0 r0 r1) divs BitVec.sdiv (fun _ => rfl)
(fun a b h => by simp [divs, h]) hat hd hb
theorem sim_sdiv_zero (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x)
(hpc : x.pc = base) (hat : RAt code base (lowerInstr L base off g .sdiv)) {a : W} {d : List W}
(hd : s.dstack = 0#32 :: a :: d) : RExec code x (.trapped .divideByZero) :=
sim_divop_zero hg hL hs hpc [.div r0 r0 r1, .store Reg.r4 4 r0, .addImm Reg.r4 4] hat hd
theorem sim_select_ok (hg : Geom c) (hL : L.dEnd = ofN c.dEnd) (hs : Rel0 c p s x)
(hpc : x.pc = base) (hat : RAt code base (lowerInstr L base off g .select)) {cc y v : W}
{d : List W} (hd : s.dstack = cc :: y :: v :: d) :
∃ x', RSteps code x x' ∧
Rel0 c p { s with dstack := (if Word.isTrue cc then v else y) :: d } x' ∧
x'.pc = base + isize g (.select : WordDialect.Instr 32) := by
have hat' : RAt code base (uf L (need g 3) base ++ [.load r1 Reg.r4 0, .load r0 Reg.r4 4,
.load r3 Reg.r4 8, .beqz r1 (base + ufSize (need g 3) + 5), .movRR r0 r3, .store Reg.r4 8 r0,
.addImm Reg.r4 8]) := hat
obtain ⟨hatuf, hatr⟩ := rat_append.mp hat'
have hufl := uf_length L (need g 3) base
rw [hufl] at hatr
obtain ⟨f0, f1, f2, f3, f4, f5, f6, _⟩ := hatr
have hk : 3 ≤ s.dstack.length := by simp [hd]
obtain ⟨x1, hs1, hr1, hpc1, hm1, _⟩ := uf_pass hg hs hL (by decide) hk hpc hatuf
obtain ⟨x2, hs2, hr2, hpc2, hv2, ho2, _⟩ := ld_step (code := code) hr1 (j := 0) (v := cc)
(by simp [hd]) (rd := r1) (by simp) (disp := 0) (by simp) (fetch_cast f0 (by omega))
obtain ⟨x3, hs3, hr3, hpc3, hv3, ho3, _⟩ := ld_step (code := code) hr2 (j := 1) (v := y)
(by simp [hd]) (rd := r0) (by simp) (disp := 4) (by simp) (fetch_cast f1 (by omega))
obtain ⟨x4, hs4, hr4, hpc4, hv4, ho4, _⟩ := ld_step (code := code) hr3 (j := 2) (v := v)
(by simp [hd]) (rd := r3) (by simp) (disp := 8) (by simp) (fetch_cast f2 (by omega))
have hcx4 : x4.regs r1 = cc := by rw [ho4 r1 (by decide), ho3 r1 (by decide), hv2]
have hax4 : x4.regs r0 = y := by rw [ho4 r0 (by decide), hv3]
-- after the branch (and the `mv` when `cc ≠ 0`), `r0` holds the result at `base + ufSize (need g 3) + 5`
obtain ⟨x6, hs6, hr6, hpc6, hval⟩ : ∃ x6, RSteps code x4 x6 ∧ Rel0 c p s x6 ∧
x6.pc = base + ufSize (need g 3) + 5 ∧ x6.regs r0 = (if Word.isTrue cc then v else y) := by
by_cases hc : cc = 0#32
· have hst : step code x4 = .next { x4 with pc := base + ufSize (need g 3) + 5 } := by
rw [rstep_of_fetch (m := x4) (fetch_cast f3 (by omega))]; simp [exec, hcx4, hc]
refine ⟨_, RSteps.single hst, hr4.congr rfl rfl rfl rfl rfl rfl rfl, rfl, ?_⟩
simp [hax4, Word.isTrue, hc]
· have hst : step code x4 = .next x4.adv := by
rw [rstep_of_fetch (m := x4) (fetch_cast f3 (by omega))]; simp [exec, hcx4, hc]
let x5 := x4.adv.mov r0 (x4.adv.regs r3)
have hst5 : step code x4.adv = .next x5 := by
rw [rstep_of_fetch (m := x4.adv) (fetch_cast f4 (by simp [M.adv]; omega))]; rfl
refine ⟨x5, (RSteps.single hst).trans (RSteps.single hst5),
mov_rel0 (hr4.congr rfl rfl rfl rfl rfl rfl rfl : Rel0 c p s x4.adv) r0
(scratch_ne r0 (by simp)) _,
by simp [x5, M.mov, M.setReg, M.adv]; omega, ?_⟩
simp [x5, M.mov, M.setReg, M.adv, Word.isTrue, hc, hv4]
obtain ⟨x7, hs7, hr7, hpc7, _⟩ := st_step (code := code) hg hr6 (j := 2) (by simp [hd])
(rs := r0) (disp := 8) (by simp) (fetch_cast f5 (by omega))
have hlen : 2 ≤ ({ s with dstack := s.dstack.set 2 (x6.regs r0) } : State 32).dstack.length := by
simp [hd]
obtain ⟨x8, hs8, hr8, hpc8, _⟩ := adj_step (code := code) hg hr7 (n := 2) hlen (δ := 8) (by simp)
(fetch_cast f6 (by omega))
have hr8' : Rel0 c p { s with dstack := (s.dstack.set 2 (x6.regs r0)).drop 2 } x8 := hr8
have e : (s.dstack.set 2 (x6.regs r0)).drop 2 = (if Word.isTrue cc then v else y) :: d := by
simp [hd, hval]
rw [e] at hr8'
exact ⟨x8, (((((hs1.trans hs2).trans hs3).trans hs4).trans hs6).trans hs7).trans hs8, hr8',
by simp [isize]; omega⟩
/-- `cmp c`: `r2 := 1`, then `bcc c` skips `r2 := 0` exactly when the IR comparison holds. -/
theorem sim_cmp_ok (cd : WordDialect.Cond) (hg : Geom c) (hL : L.dEnd = ofN c.dEnd)
(hs : Rel0 c p s x) (hpc : x.pc = base) (hat : RAt code base (lowerInstr L base off g (.cmp cd)))
{b a : W} {d : List W} (hd : s.dstack = b :: a :: d) :
∃ x', RSteps code x x' ∧
Rel0 c p { s with dstack := Word.ofBool (cd.eval a b) :: d } x' ∧
x'.pc = base + isize g (.cmp cd : WordDialect.Instr 32) := by
have hat' : RAt code base (uf L (need g 2) base ++ [.load r1 Reg.r4 0, .load r0 Reg.r4 4,
.movImm r2 1#32, .bcc cd r0 r1 (base + ufSize (need g 2) + 5), .movImm r2 0#32, .store Reg.r4 4 r2,
.addImm Reg.r4 4]) := hat
obtain ⟨hatuf, hatr⟩ := rat_append.mp hat'
have hufl := uf_length L (need g 2) base
rw [hufl] at hatr
obtain ⟨f0, f1, f2, f3, f4, f5, f6, _⟩ := hatr
have hk : 2 ≤ s.dstack.length := by simp [hd]
obtain ⟨x1, hs1, hr1, hpc1, hm1, _⟩ := uf_pass hg hs hL (by decide) hk hpc hatuf
obtain ⟨x2, hs2, hr2, hpc2, hv2, ho2, _⟩ := ld_step (code := code) hr1 (j := 0) (v := b)
(by simp [hd]) (rd := r1) (by simp) (disp := 0) (by simp) (fetch_cast f0 (by omega))
obtain ⟨x3, hs3, hr3, hpc3, hv3, ho3, _⟩ := ld_step (code := code) hr2 (j := 1) (v := a)
(by simp [hd]) (rd := r0) (by simp) (disp := 4) (by simp) (fetch_cast f1 (by omega))
have hcx3 : x3.regs r1 = b := by rw [ho3 r1 (by decide), hv2]
obtain ⟨x4, hs4, hr4, hpc4, hv4, ho4, _⟩ := movimm_step (code := code) hr3 (rd := r2) (by simp)
(v := 1#32) (fetch_cast f2 (by omega))
have hax4 : x4.regs r0 = a := by rw [ho4 r0 (by decide), hv3]
have hcx4 : x4.regs r1 = b := by rw [ho4 r1 (by decide), hcx3]
obtain ⟨x6, hs6, hr6, hpc6, hval⟩ : ∃ x6, RSteps code x4 x6 ∧ Rel0 c p s x6 ∧
x6.pc = base + ufSize (need g 2) + 5 ∧ x6.regs r2 = Word.ofBool (cd.eval a b) := by
by_cases hc : cd.eval a b = true
· have hst : step code x4 = .next { x4 with pc := base + ufSize (need g 2) + 5 } := by
rw [rstep_of_fetch (m := x4) (fetch_cast f3 (by omega))]; simp [exec, hax4, hcx4, hc]
refine ⟨_, RSteps.single hst, hr4.congr rfl rfl rfl rfl rfl rfl rfl, rfl, ?_⟩
simp [hv4, hc, Word.ofBool]
· have hc' : cd.eval a b = false := by simpa using hc
have hst : step code x4 = .next x4.adv := by
rw [rstep_of_fetch (m := x4) (fetch_cast f3 (by omega))]; simp [exec, hax4, hcx4, hc']
obtain ⟨x5, hs5, hr5, hpc5, hv5, _, _⟩ := movimm_step (code := code)
(hr4.congr rfl rfl rfl rfl rfl rfl rfl : Rel0 c p s x4.adv) (rd := r2) (by simp)
(v := 0#32) (fetch_cast f4 (by simp [M.adv]; omega))
refine ⟨x5, (RSteps.single hst).trans hs5, hr5, by simp [M.adv] at hpc5; omega, ?_⟩
simp [hv5, hc', Word.ofBool]
obtain ⟨x7, hs7, hr7, hpc7, _⟩ := st_step (code := code) hg hr6 (j := 1) (by simp [hd])
(rs := r2) (disp := 4) (by simp) (fetch_cast f5 (by omega))
have hlen : 1 ≤ ({ s with dstack := s.dstack.set 1 (x6.regs r2) } : State 32).dstack.length := by
simp [hd]
obtain ⟨x8, hs8, hr8, hpc8, _⟩ := adj_step (code := code) hg hr7 (n := 1) hlen (δ := 4) (by simp)
(fetch_cast f6 (by omega))
have hr8' : Rel0 c p { s with dstack := (s.dstack.set 1 (x6.regs r2)).drop 1 } x8 := hr8
have e : (s.dstack.set 1 (x6.regs r2)).drop 1 = Word.ofBool (cd.eval a b) :: d := by
simp [hd, hval]
rw [e] at hr8'
exact ⟨x8, (((((hs1.trans hs2).trans hs3).trans hs4).trans hs6).trans hs7).trans hs8, hr8',
by simp [isize]; omega⟩
end Div
end CM
end WordDialect
|