File size: 6,268 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 | 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 | import CM.Lower
/-!
# CM.Run
Run relations for the Thumb-2 model (`RSteps`, `RExec`), code placement (`RAt`), and the lemma
locating each IR instruction's lowered code inside `lowerProg`.
-/
namespace WordDialect
namespace CM
inductive RSteps (code : List (Instr Nat)) : M β M β Prop where
| refl {m : M} : RSteps code m m
| cons {m m1 m2 : M} : step code m = .next m1 β RSteps code m1 m2 β RSteps code m m2
theorem RSteps.single {code : List (Instr Nat)} {m m1 : M} (h : step code m = .next m1) :
RSteps code m m1 := .cons h .refl
theorem RSteps.trans {code : List (Instr Nat)} {m m1 m2 : M}
(hβ : RSteps code m m1) (hβ : RSteps code m1 m2) : RSteps code m m2 := by
induction hβ with
| refl => exact hβ
| cons hs _ ih => exact .cons hs (ih hβ)
/-- Running to a final outcome. -/
inductive RExec (code : List (Instr Nat)) : M β Out β Prop where
| halt {m m' : M} : step code m = .halted m' β RExec code m (.halted m')
| trap {m : M} {t : Trap} : step code m = .trapped t β RExec code m (.trapped t)
| ovf {m : M} : step code m = .overflow β RExec code m .overflow
| next {m m1 : M} {o : Out} : step code m = .next m1 β RExec code m1 o β RExec code m o
theorem RExec.of_steps {code : List (Instr Nat)} {m m1 : M} {o : Out}
(hs : RSteps code m m1) (he : RExec code m1 o) : RExec code m o := by
induction hs with
| refl => exact he
| cons h _ ih => exact .next h (ih he)
/-- `seg` occurs in `code` starting at index `pc`. -/
def RAt : List (Instr Nat) β Nat β List (Instr Nat) β Prop
| _, _, [] => True
| code, pc, i :: is => code[pc]? = some i β§ RAt code (pc + 1) is
theorem rat_append {code : List (Instr Nat)} {pc : Nat} {cβ cβ : List (Instr Nat)} :
RAt code pc (cβ ++ cβ) β RAt code pc cβ β§ RAt code (pc + cβ.length) cβ := by
induction cβ generalizing pc with
| nil => simp [RAt]
| cons i is ih =>
simp only [List.cons_append, RAt, ih, List.length_cons]
constructor
Β· rintro β¨h, h1, h2β©; refine β¨β¨h, h1β©, ?_β©
have e : pc + 1 + is.length = pc + (is.length + 1) := by omega
rw [β e]; exact h2
Β· rintro β¨β¨h, h1β©, h2β©; refine β¨h, h1, ?_β©
have e : pc + 1 + is.length = pc + (is.length + 1) := by omega
rw [e]; exact h2
theorem fetch_cast {code : List (Instr Nat)} {i : Instr Nat} {a b : Nat}
(h : code[a]? = some i) (hab : a = b) : code[b]? = some i := hab βΈ h
theorem rstep_of_fetch {code : List (Instr Nat)} {m : M} {i : Instr Nat}
(h : code[m.pc]? = some i) : step code m = exec i m := by
simp [step, h]
/-- Prefix sums: `offs` of a cons. -/
theorem offs_cons (cf : Nat β Facts) (i : WordDialect.Instr 32) (is : Prog 32) (k : Nat) :
offs cf (i :: is) (k + 1) = isize (cf 0) i + offs (fun j => cf (j + 1)) is k := rfl
theorem offs_zero (cf : Nat β Facts) (p : Prog 32) : offs cf p 0 = 0 := by cases p <;> rfl
theorem rat_embed (pre seg post : List (Instr Nat)) : RAt (pre ++ seg ++ post) pre.length seg := by
induction seg generalizing pre with
| nil => simp [RAt]
| cons i is ih =>
refine β¨by simp, ?_β©
have := ih (pre ++ [i])
simpa [List.append_assoc] using this
/-- Where the lowered code of IR instruction `k` sits: at index `pre.length + offs cf ps k`, with
the guards for the facts `cf k`. -/
theorem lowerGo_at (L : Layout) (off : Nat β Nat) :
β (ps : Prog 32) (cf : Nat β Facts) (pre : List (Instr Nat)) (k : Nat) (i : WordDialect.Instr 32),
ps[k]? = some i β
RAt (pre ++ lowerGo L off cf pre.length ps) (pre.length + offs cf ps k)
(lowerInstr L (pre.length + offs cf ps k) off (cf k) i) := by
intro ps
induction ps with
| nil => intro cf pre k i h; simp at h
| cons j js ih =>
intro cf pre k i h
cases k with
| zero =>
simp at h; subst h
simp only [offs_zero, Nat.add_zero, lowerGo]
have := rat_embed pre (lowerInstr L pre.length off (cf 0) j)
(lowerGo L off (fun j => cf (j + 1)) (pre.length + isize (cf 0) j) js)
simpa [List.append_assoc] using this
| succ k =>
simp only [List.getElem?_cons_succ] at h
have := ih (fun j => cf (j + 1)) (pre ++ lowerInstr L pre.length off (cf 0) j) k i h
have hl : (pre ++ lowerInstr L pre.length off (cf 0) j).length = pre.length + isize (cf 0) j := by
simp [lowerInstr_length]
rw [hl] at this
simp only [lowerGo, offs_cons]
have e : pre.length + (isize (cf 0) j + offs (fun j => cf (j + 1)) js k) =
pre.length + isize (cf 0) j + offs (fun j => cf (j + 1)) js k := by omega
rw [e]
simpa [List.append_assoc] using this
theorem lowerProg_at (L : Layout) (p : Prog 32) (k : Nat) (i : WordDialect.Instr 32)
(h : p[k]? = some i) :
RAt (lowerProg L p) (offs L.facts p k) (lowerInstr L (offs L.facts p k) (offs L.facts p) (L.facts k) i) := by
have := lowerGo_at L (offs L.facts p) p L.facts [] k i h
simpa [lowerProg] using this
/-- The final stub (bad-pc trap) sits at `offs cf p p.length`. -/
theorem lowerGo_stub (L : Layout) (off : Nat β Nat) :
β (ps : Prog 32) (cf : Nat β Facts) (pre : List (Instr Nat)),
RAt (pre ++ lowerGo L off cf pre.length ps) (pre.length + offs cf ps ps.length)
[.exitTrap .badPc] := by
intro ps
induction ps with
| nil =>
intro cf pre
simp only [lowerGo, List.length_nil, offs_zero, Nat.add_zero]
have := rat_embed pre [.exitTrap .badPc] []
simpa using this
| cons j js ih =>
intro cf pre
have := ih (fun j => cf (j + 1)) (pre ++ lowerInstr L pre.length off (cf 0) j)
have hl : (pre ++ lowerInstr L pre.length off (cf 0) j).length = pre.length + isize (cf 0) j := by
simp [lowerInstr_length]
rw [hl] at this
simp only [lowerGo, List.length_cons, offs_cons]
have e : pre.length + (isize (cf 0) j + offs (fun j => cf (j + 1)) js js.length) =
pre.length + isize (cf 0) j + offs (fun j => cf (j + 1)) js js.length := by omega
rw [e]
simpa [List.append_assoc] using this
theorem lowerProg_stub (L : Layout) (p : Prog 32) :
RAt (lowerProg L p) (offs L.facts p p.length) [.exitTrap .badPc] := by
have := lowerGo_stub L (offs L.facts p) p L.facts []
simpa [lowerProg] using this
end CM
end WordDialect
|