aboutsummaryrefslogtreecommitdiff
path: root/src/Assembly/Pseudo.v
blob: 6bb6199107fa2fe9324fcb4c5a093e7984477ca7 (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
Require Import QhasmEvalCommon QhasmUtil.
Require Import Language.
Require Import List.

Module Type PseudoMachine.
  Parameter width: nat.
  Parameter vars: nat.
  Parameter width_spec: ISize width.
End PseudoMachine.

Module Pseudo (M: PseudoMachine) <: Language.
  Import ListNotations State Util M.

  Definition const: Type := word width.

  (* Program Types *)
  Definition State := list const.

  Inductive Pseudo: nat -> nat -> Type :=
    | PVar: forall n, Index n -> Pseudo n 1
    | PConst: forall n, const -> Pseudo n 1

    | PBin: forall n, IntOp -> Pseudo n 2 -> Pseudo n 1
    | PDual: forall n, DualOp -> Pseudo n 2 -> Pseudo n 2
    | PShift: forall n, RotOp -> Index width -> Pseudo n 1 -> Pseudo n 1

    | PLet: forall n k m, Pseudo n k -> Pseudo (n + k) m -> Pseudo n m
    | PComb: forall n a b, Pseudo n a -> Pseudo n b -> Pseudo n (a + b)

    | PIf: forall n m, TestOp -> Index n -> Index n -> Pseudo n m -> Pseudo n m -> Pseudo n m
    | PFunExp: forall n, Pseudo n n -> nat -> Pseudo n n.

  Hint Constructors Pseudo.

  Definition Program := Pseudo vars vars.

  Definition applyBin (op: IntOp) (a b: word width): const :=
    match op with
    | IPlus => wplus a b
    | IMinus => wminus a b
    | IAnd => wand a b
    | IXor => wxor a b
    | IOr => wor a b
    end.

  Definition applyDual (op: DualOp) (a b: word width): list const :=
    match op with
    | Wmult =>
      let wres := natToWord (width + width)
        (N.to_nat ((wordToN a) * (wordToN b))) in
      [split1 _ _ wres; split2 _ _ wres]
    end.

  Definition applyShift (op: RotOp) (v: const) (n: Index width): const.
    refine match op with
    | Shl => convert (Word.combine (wzero n) (split1 (width - n) n (convert v _))) _
    | Shr => convert (Word.combine (split2 n (width - n) (convert v _)) (wzero n)) _
    end; abstract (
      unfold const;
      destruct n; simpl;
      replace (width - x + x) with width;
      replace (x + (width - x)) with width;
      intuition ).
  Defined.

  Fixpoint pseudoEval {n m} (prog: Pseudo n m) (st: State): option State :=
    let option_map' := fun {A B: Type} (x: option A) (f: A -> option B) =>
      match x with
      | Some x' =>
        match (f x') with
        | Some res => Some res
        | None => None
        end
      | _ => None
      end in

    match prog with
    | PVar n i => option_map' (nth_error st i) (fun x => Some [x])
    | PConst n c => Some ([c])

    | PBin n o p =>
      option_map' (pseudoEval p st) (fun sp =>
        match sp with
        | [wa; wb] => Some [applyBin o wa wb]
        | _ => None
        end)

    | PDual n o p =>
      option_map' (pseudoEval p st) (fun sp =>
        match sp with
        | [wa; wb] => Some (applyDual o wa wb)
        | _ => None
        end)

    | PShift n o a x =>
      option_map' (pseudoEval x st) (fun sx =>
        match sx with
        | [wx] => Some [applyShift o wx a]
        | _ => None
        end)

    | PLet n k m f g =>
      option_map' (pseudoEval f st) (fun sf =>
        option_map' (pseudoEval g (st ++ sf))
          (fun sg => Some sg))

    | PComb n a b f g =>
      option_map' (pseudoEval f st) (fun sf =>
        option_map' (pseudoEval g st) (fun sg =>
          Some (sf ++ sg)))

    | PIf n m t i0 i1 l r =>
      option_map' (nth_error st i0) (fun v0 =>
        option_map' (nth_error st i1) (fun v1 =>
          if (evalTest t v0 v1)
          then pseudoEval l st
          else pseudoEval r st ))

    | PFunExp n p e =>
      (fix funexpseudo (e': nat) (st': State) := 
        match e' with
        | O => Some st'
        | S e'' =>
          option_map' (pseudoEval p st') (fun st'' =>
            funexpseudo e'' st'')
        end) e st
    end.

  Definition evaluatesTo := fun (p: Program) (st st': State) => (pseudoEval p st = Some st').

  (* world peace *)
End Pseudo.

Module PseudoUnary32 <: PseudoMachine.
  Definition width := 32.
  Definition vars := 1.
  Definition width_spec := I32.
  Definition const: Type := word width.
End PseudoUnary32.

Module PseudoUnary64 <: PseudoMachine.
  Definition width := 64.
  Definition vars := 1.
  Definition width_spec := I64.
  Definition const: Type := word width.
End PseudoUnary64.