aboutsummaryrefslogtreecommitdiff
path: root/src/Assembly/QhasmEvalCommon.v
blob: 9760dc8690e403c6544776ecb406739bd0017336 (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
Require Import Crypto.Assembly.QhasmCommon Crypto.Assembly.QhasmUtil Crypto.Assembly.State.
Require Import Coq.ZArith.ZArith Coq.Bool.Sumbool.
Require Import Bedrock.Word.
Require Import Coq.Logic.Eqdep_dec Coq.Logic.ProofIrrelevance.
Require Export Crypto.Util.FixCoqMistakes.

Module EvalUtil.
  Definition evalTest {n} (o: TestOp) (a b: word n): bool :=
    let c := (N.compare (wordToN a) (wordToN b)) in

    let eqBit := match c with | Eq => true | _ => false end in
    let ltBit := match c with | Lt => true | _ => false end in
    let gtBit := match c with | Gt => true | _ => false end in

    match o with
    | TEq => eqBit
    | TLt => ltBit
    | TLe => orb (eqBit) (ltBit)
    | TGt => gtBit
    | TGe => orb (eqBit) (gtBit)
    end.

  Definition evalIntOp {b} (io: IntOp) (x y: word b) :=
    match io return (word b) * option bool with
    | ISub => (wminus x y, None)
    | IXor => (wxor x y, None)
    | IAnd => (wand x y, None)
    | IOr => (wor x y, None)
    | IAdd =>
      let v := (wordToN x + wordToN y)%N in
      let c := (overflows b (&x + &y)%N)%w in

      match c as c' return c' = c -> _ with
      | right _ => fun _ => (NToWord b v, Some false)
      | left _ => fun _ => (NToWord b v, Some true)
      end eq_refl
    end.

  Definition evalCarryOp {b} (io: CarryOp) (x y: word b) (c: bool): (word b) * bool :=
    match io with
    | AddWidthCarry =>
      let c' := (overflows b (&x + &y + (if c then 1 else 0))%N)%w in
      let v := addWithCarry x y c in

      match c' as c'' return c' = c'' -> _ with
      | right _ => fun _ => (v, false)
      | left _ => fun _ => (v, true)
      end eq_refl
    end.

  Definition highBits {n} (m: nat) (x: word n) := snd (break m x).

  Definition multHigh {n} (x y: word n): word n.
    refine (@extend _ n _ ((highBits (n/2) x) ^* (highBits (n/2) y)));
      abstract omega.
  Defined.

  Definition evalDualOp {n} (duo: DualOp) (x y: word n) :=
    match duo with
    | Mult => (x ^* y, multHigh x y)
    end.

  Definition evalRotOp {b} (ro: RotOp) (x: word b) (n: nat) :=
    match ro with
    | Shl => NToWord b (N.shiftl_nat (wordToN x) n)
    | Shr => NToWord b (N.shiftr_nat (wordToN x) n)
    end.

  (* Width decideability *)

  Definition getWidth (n: nat): option (Width n) :=
    match n with
    | 32 => Some W32
    | 64 => Some W64
    | _ => None
    end.

  Lemma getWidth_eq {n} (a: Width n): Some a = getWidth n.
  Proof. induction a; unfold getWidth; simpl; intuition. Qed.

  Lemma width_eq {n} (a b: Width n): a = b.
  Proof.
    assert (Some a = Some b) as H by (
      replace (Some a) with (getWidth n) by (rewrite getWidth_eq; intuition);
      replace (Some b) with (getWidth n) by (rewrite getWidth_eq; intuition);
      intuition).
    inversion H; intuition.
  Qed.

  (* Mapping Conversions *)

  Definition wordToM {n: nat} {spec: Width n} (w: word n): Mapping n :=
    constM _ (constant spec w).

  Definition regToM {n: nat} {spec: Width n} (r: Reg n): Mapping n :=
    regM _ r.

  Definition stackToM {n: nat} {spec: Width n} (s: Stack n): Mapping n :=
    stackM _ s.

  Definition constToM {n: nat} {spec: Width n} (c: Const n): Mapping n :=
    constM _ c.

  Definition mapping_dec {n} (a b: Mapping n): {a = b} + {a <> b}.
    refine (match (a, b) as p' return (a, b) = p' -> _ with
    | (regM v, regM v') => fun _ =>
        if (Nat.eq_dec (regName v) (regName v'))
        then left _
        else right _

    | (stackM v, stackM v') => fun _ =>
        if (Nat.eq_dec (stackName v) (stackName v'))
        then left _
        else right _

    | (constM v, constM v') => fun _ =>
        if (Nat.eq_dec (constValueN v) (constValueN v'))
        then left _
        else right _

    | (memM _ v i, memM _ v' i') => fun _ =>
        if (Nat.eq_dec (memName v) (memName v'))
        then if (Nat.eq_dec (memLength v) (memLength v'))
        then if (Nat.eq_dec (proj1_sig i) (proj1_sig i'))
        then left _ else right _ else right _ else right _

    | _ => fun _ => right _
    end (eq_refl (a, b)));
      try destruct v, v'; subst;
      unfold regName, stackName, constValueN, memName, memLength in *;
      repeat progress (try apply f_equal; subst; match goal with
        (* Makeshift intuition *)
      | [ |- ?x = ?x ] => reflexivity
      | [ H: ?x <> ?x |- _ ] => destruct H
      | [ |- ?x = ?y ] => apply proof_irrelevance

        (* Destruct the widths *)
      | [ w0: Width ?x, w1: Width ?x |- _ ] =>
        let H := fresh in
        assert (w0 = w1) as H by (apply width_eq);
          rewrite H in *;
          clear w0 H

        (* Invert <> *)
      | [ |- regM _ _ <> _ ] => let H := fresh in (intro H; inversion H)
      | [ |- memM _ _ _ <> _ ] => let H := fresh in (intro H; inversion H)
      | [ |- stackM _ _ <> _ ] => let H := fresh in (intro H; inversion H)
      | [ |- constM _ _ <> _ ] => let H := fresh in (intro H; inversion H)

        (* Invert common structures *)
      | [ H: regName _ = regName _ |- _ ] => inversion_clear H
      | [ H: (_, _) = _ |- _ ] => inversion_clear H
      | [ H: ?x = _ |- _ ] => is_var x; rewrite H in *; clear H

        (* Destruct sigmas, exist, existT *)
      | [ H: proj1_sig ?a = proj1_sig ?b |- _ ] =>
        let l0 := fresh in let l1 := fresh in
        destruct a, b; simpl in H; subst
      | [ H: proj1_sig ?a <> proj1_sig ?b |- _ ] =>
        let l0 := fresh in let l1 := fresh in
        destruct a, b; simpl in H; subst
      | [ H: existT ?a ?b _ = existT ?a ?b _ |- _ ] =>
        apply (inj_pair2_eq_dec _ Nat.eq_dec) in H;
            subst; intuition
      | [ H: exist _ _ _ = exist _ _ _ |- _ ] =>
        inversion H; subst; intuition

        (* Single specialized wordToNat proof *)
      | [ H: wordToNat ?a = wordToNat ?b |- ?a = ?b] =>
        rewrite <- (natToWord_wordToNat a);
        rewrite <- (natToWord_wordToNat b);
        rewrite H; reflexivity

      | _ => idtac
      end).
  Defined.

  Definition dec_lt (a b: nat): {(a < b)%nat} + {(a >= b)%nat}.
    assert ({(a <? b)%nat = true} + {(a <? b)%nat <> true})
      by abstract (destruct (a <? b)%nat; intuition auto with bool);
      destruct H.

    - left; abstract (apply Nat.ltb_lt; intuition).

    - right; abstract (rewrite Nat.ltb_lt in *; intuition auto with zarith).
  Defined.

  Fixpoint stackNames {n} (lst: list (Mapping n)): list nat :=
    match lst with
    | nil => nil
    | cons c cs =>
      match c with
      | stackM v => cons (stackName v) (stackNames cs)
      | _ => stackNames cs
      end
    end.

  Fixpoint regNames {n} (lst: list (Mapping n)): list nat :=
    match lst with
    | nil => nil
    | cons c cs =>
      match c with
      | regM v => cons (regName v) (regNames cs)
      | _ => regNames cs
      end
    end.

End EvalUtil.

Module QhasmEval.
  Export EvalUtil FullState.

  Definition evalCond (c: Conditional) (state: State): option bool :=
    match c with
    | CTrue => Some true
    | CZero n r =>
      omap (getReg r state) (fun v =>
        if (Nat.eq_dec O (wordToNat v))
        then Some true
        else Some false)
    | CReg n o a b =>
      omap (getReg a state) (fun va =>
        omap (getReg b state) (fun vb =>
          Some (evalTest o va vb)))
    | CConst n o a c =>
      omap (getReg a state) (fun va =>
        Some (evalTest o va (constValueW c)))
    end.

  Definition evalOperation (o: Operation) (state: State): option State :=
    match o with
    | IOpConst _ o r c =>
      omap (getReg r state) (fun v =>
        let (v', co) := (evalIntOp o v (constValueW c)) in
        Some (setCarryOpt co (setReg r v' state)))

    | IOpReg _ o a b =>
      omap (getReg a state) (fun va =>
        omap (getReg b state) (fun vb =>
            let (v', co) := (evalIntOp o va vb) in
            Some (setCarryOpt co (setReg a v' state))))

    | IOpStack _ o a b =>
      omap (getReg a state) (fun va =>
        omap (getStack b state) (fun vb =>
            let (v', co) := (evalIntOp o va vb) in
            Some (setCarryOpt co (setReg a v' state))))

    | IOpMem _ _ o r m i =>
      omap (getReg r state) (fun va =>
        omap (getMem m i state) (fun vb =>
            let (v', co) := (evalIntOp o va vb) in
            Some (setCarryOpt co (setReg r v' state))))

    | DOp _ o a b (Some x) =>
      omap (getReg a state) (fun va =>
        omap (getReg b state) (fun vb =>
            let (low, high) := (evalDualOp o va vb) in
            Some (setReg x high (setReg a low state))))

    | DOp _ o a b None =>
      omap (getReg a state) (fun va =>
        omap (getReg b state) (fun vb =>
            let (low, high) := (evalDualOp o va vb) in
            Some (setReg a low state)))

    | ROp _ o r i =>
      omap (getReg r state) (fun v =>
        let v' := (evalRotOp o v i) in
        Some (setReg r v' state))

    | COp _ o a b =>
      omap (getReg a state) (fun va =>
        omap (getReg b state) (fun vb =>
            match (getCarry state) with
            | None => None
            | Some c0 =>
            let (v', c') := (evalCarryOp o va vb c0) in
            Some (setCarry c' (setReg a v' state))
            end))
    end.

  Definition evalAssignment (a: Assignment) (state: State): option State :=
    match a with
    | ARegMem _ _ r m i =>
      omap (getMem m i state) (fun v => Some (setReg r v state))
    | AMemReg _ _ m i r =>
      omap (getReg r state) (fun v => Some (setMem m i v state))
    | AStackReg _ a b =>
      omap (getReg b state) (fun v => Some (setStack a v state))
    | ARegStack _ a b =>
      omap (getStack b state) (fun v => Some (setReg a v state))
    | ARegReg _ a b =>
      omap (getReg b state) (fun v => Some (setReg a v state))
    | AConstInt _ r c =>
      Some (setReg r (constValueW c) state)
    end.

End QhasmEval.