aboutsummaryrefslogtreecommitdiff
path: root/src/Assembly/PseudoMedialConversion.v
blob: a2ec6fe40782cae562ff73afbe4facae826be9e2 (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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445

Require Export Language Conversion QhasmEvalCommon QhasmUtil.
Require Export Pseudo AlmostQhasm State.
Require Export Bedrock.Word NArith NPeano Euclid.
Require Export List Sumbool Vector.

Module PseudoConversion (Arch: PseudoMachine).
  Import QhasmCommon AlmostQhasm EvalUtil Util.
  Import ListNotations.

  Module P := Pseudo Arch.
  (* Module M := Medial Arch. *)

  (******************    Material Conversions    ************************)

  Module PseudoConversion <: Conversion P AlmostQhasm.
    Import P (* M *) Arch StateCommon.

    (* Fixpoint convertState (st: M.State): option P.State :=
      let try_cons := fun {T} (x: option T) (l: list T) =>
        match x with | Some x' => x' :: l | _ => l end in

      match st with
      | (v, m, c) =>
        let varList := (fix cs' (n: nat) :=
          try_cons (option_map (NToWord width) (NatM.find n v))
            (match n with | O => [] | S m => cs' m end)) vars in

        if (Nat.eq_dec (length varList) vars)
        then Some (varList, m, c)
        else None
      end. *)

    Fixpoint range (start len: nat): list nat :=
      match len with
      | O => []
      | S len' => start :: (range (S start) len')
      end.

    Fixpoint list_split {A} (n: nat) (lst: list A): (list A) * (list A) :=
      match n with
      | O => ([], lst)
      | S m =>
        match lst with
        | [] => ([], [])
        | l :: ls =>
          match list_split m ls with
          | (a, b) => (l :: a, b)
          end
        end
      end.

    Fixpoint maxN (lst: list nat): nat :=
      match lst with
      | x :: xs => max x (maxN xs)
      | [] => O
      end.

    Definition MMap := NatM.t (Mapping width).
    Definition mempty := NatM.empty (Mapping width).

    Fixpoint convertProgram' {n m} (prog: Pseudo n m) (start: nat) (M: MMap):
        option (AlmostProgram * (list (Mapping width)) * MMap) :=
      let rM := fun (x: nat) => regM _ (reg width_spec x) in
      let sM := fun (x: nat) => stackM _ (stack width_spec x) in

      let g := fun (k: nat) =>
        match (NatM.find k M) with
        | Some v => v
        | _ => rM k (* TODO: more intelligent defaults *)
        end in

      let madd := fun (k: nat) (f: nat -> Mapping width) => NatM.add k (f k) M in

      match prog with
      | PVar n (Some true) i => Some (ASkip, [rM i], madd i rM)
      | PVar n (Some false) i => Some (ASkip, [sM i], madd i sM)
      | PVar n None i => Some (ASkip, [g i], M) 
      | PConst n c => Some (AAssign (AConstInt (rM start) c), [rM start], madd start rM)
      | PMem n m v i => Some (AAssign (ARegMem (rM start) v i), madd start rM)
      | _ => None
      end.


      | PBin n o p =>
        match (convertProgram' p start) with
        | Some (p', [a; b]) =>
          Some (MSeq p' (MOp (MIOpReg o a b)), [a])
        | _ => None
        end

      | PCarry n o p =>
        match (convertProgram' p start) with
        | Some (p', [a; b]) =>
          Some (MSeq p' (MOp (MCOpReg o a b)), [a])
        | _ => None
        end

      | PDual n o p =>
        match (convertProgram' p start) with
        | Some (p', [a; b]) =>
          let nextOpen := S (maxN [a; b]) in
          Some (MSeq p' (MOp (MDOpReg o a b (Some nextOpen))), [a; nextOpen])
        | _ => None
        end

      | PShift n o a x =>
        match (convertProgram' x start) with
        | Some (p', [r]) =>
          Some (MSeq p' (MOp (MOpRot o r a)), [proj1_sig a])
        | _ => None
        end

      | PLet n k m f g =>
        ft <- convertProgram' f (start + k);
        gt <- convertProgram' g (S (maxN (snd ft)));

        match (ft, gt) with
        | ((fp, fr), (gp, gr)) => Some (MSeq fp gp, gr)
        end

      | PComb n a b f g => 
        ft <- convertProgram' f start;
        gt <- convertProgram' g (S (maxN (snd ft)));
        match (ft, gt) with
        | ((fp, fr), (gp, gr)) => Some (MSeq fp gp, fr ++ gr)
        end

      | PIf n m o i0 i1 l r => 
        lt <- convertProgram' l start;
        rt <- convertProgram' r start;
        match (lt, rt) with
        | ((lp, lr), (rp, rr)) => Some (MCond (MCReg o i0 i1) lp rp, lr)
        end
        (* TODO: prove that all (Pseudo n m) will return the same list *)

      | PFunExp n f e => 
        match (convertProgram' f start) with
        | Some (fp, fr) => Some (MFunexp e fp, fr)
        | _ => None
        end
 
      | PCall n m lbl _ => Some (MCall lbl, range n m)
      end.

    (* Results are in the locations described by the
       returned list. start is the next known open address *)
    Fixpoint convertProgram' {n m} (prog: Pseudo n m) (start: nat):
        option (Medial * (list nat)) :=

      match prog with
      | PVar n i => Some (MSkip, [start])
      | PConst n c => Some (MAssign (MAConst start c), [start])
      | PMem n m v i => Some (MAssign (MAMem _ start v i), [start])

      | PBin n o p =>
        match (convertProgram' p start) with
        | Some (p', [a; b]) =>
          Some (MSeq p' (MOp (MIOpReg o a b)), [a])
        | _ => None
        end

      | PCarry n o p =>
        match (convertProgram' p start) with
        | Some (p', [a; b]) =>
          Some (MSeq p' (MOp (MCOpReg o a b)), [a])
        | _ => None
        end

      | PDual n o p =>
        match (convertProgram' p start) with
        | Some (p', [a; b]) =>
          let nextOpen := S (maxN [a; b]) in
          Some (MSeq p' (MOp (MDOpReg o a b (Some nextOpen))), [a; nextOpen])
        | _ => None
        end

      | PShift n o a x =>
        match (convertProgram' x start) with
        | Some (p', [r]) =>
          Some (MSeq p' (MOp (MOpRot o r a)), [proj1_sig a])
        | _ => None
        end

      | PLet n k m f g =>
        ft <- convertProgram' f (start + k);
        gt <- convertProgram' g (S (maxN (snd ft)));

        match (ft, gt) with
        | ((fp, fr), (gp, gr)) => Some (MSeq fp gp, gr)
        end

      | PComb n a b f g => 
        ft <- convertProgram' f start;
        gt <- convertProgram' g (S (maxN (snd ft)));
        match (ft, gt) with
        | ((fp, fr), (gp, gr)) => Some (MSeq fp gp, fr ++ gr)
        end

      | PIf n m o i0 i1 l r => 
        lt <- convertProgram' l start;
        rt <- convertProgram' r start;
        match (lt, rt) with
        | ((lp, lr), (rp, rr)) => Some (MCond (MCReg o i0 i1) lp rp, lr)
        end
        (* TODO: prove that all (Pseudo n m) will return the same list *)

      | PFunExp n f e => 
        match (convertProgram' f start) with
        | Some (fp, fr) => Some (MFunexp e fp, fr)
        | _ => None
        end
 
      | PCall n m lbl _ => Some (MCall lbl, range n m)
      end.

    Definition addFunMap {T} (a b: TripleM.t T): TripleM.t T :=
      (fix add' (m: TripleM.t T) (elts: list ((nat * nat * nat) * T)%type) :=
        match elts with
        | [] => m
        | (k, v) :: elts' => add' (TripleM.add k v m) elts'
        end) a (TripleM.elements b).

    Definition convertProgram (prog: Pseudo vars vars): option M.Program :=
      let defs: TripleM.t M.Program :=
        (fix allDefs {n m: nat} (prog: Pseudo n m): TripleM.t M.Program :=
          match prog with
          | PBin n o p => allDefs p
          | PCarry n o p => allDefs p
          | PDual n o p => allDefs p
          | PShift n o a x => allDefs x
          | PLet n k m f g => addFunMap (allDefs f) (allDefs g)
          | PComb n a b f g => addFunMap (allDefs f) (allDefs g)
          | PIf n m t i0 i1 l r => addFunMap (allDefs l) (allDefs r)
          | PFunExp n p e => allDefs p
          | PCall n m l p =>
            match (convertProgram' p n) with
            | Some (mp, _) => TripleM.add (n, m, l) mp (allDefs p)
            | _ => allDefs p
            end
          | _ => TripleM.empty M.Program
          end) _ _ prog in

     let foldF := fun (p: M.Program) (t: (nat * nat * nat) * M.Program) =>
       match t with
       | ((n', m', lbl), p') => MDef lbl p' p
       end in

     let f: M.Program -> option M.Program := fun x =>
       Some (fold_left foldF x (of_list (TripleM.elements defs))) in

     match (convertProgram' prog vars) with
     | Some (p', _) => f p'
     | _ => None
     end.

    Lemma convert_spec:  forall a a' b b' prog prog',
      convertProgram prog = Some prog' ->
      convertState a = Some a' -> convertState b = Some b' ->
      M.evaluatesTo prog' a b <-> P.evaluatesTo prog a' b'.
    Admitted.

  End PseudoConversion.

  Module MedialConversion <: Conversion M AlmostQhasm.
    Import Arch M FullState.

    Definition ireg (x: nat): Reg width := reg width_spec x.
    Definition iconst (x: word width): Const width := const width_spec x.
    Definition istack (x: nat): Stack width := stack width_spec x.

    Definition tmpMap: nat -> Mapping width := (fun x => regM width (ireg x)).

    Definition getMapping (m: Mapping width) (st: AlmostQhasm.State): option (word width) :=
      match m with
      | regM r => getReg r st
      | stackM s => getStack s st
      | _ => None
      end.

    Definition convertState' (mapF: nat -> Mapping width) (st: AlmostQhasm.State): option M.State :=
      let tryAddVar := fun (k: nat) (x: option (word width)) (st: M.State) =>
        match x with | Some x' => MedState.setVar k x' st | _ => st end in

      Some ((fix cs' (n: nat) :=
           tryAddVar n (getMapping (mapF n) st)
           (match n with | O => MedState.emptyState | S m => cs' m end))
         vars).

    Definition convertState (st: AlmostQhasm.State): option M.State :=
      convertState' tmpMap st.

    Fixpoint convertProgram'
             (prog: M.Program)
             (mapF: nat -> Mapping width)
             (nextFree tmp: nat):
        option AlmostQhasm.Program :=

      let omap := fun {A B} (x: option A) (f: A -> option B) =>
        match x with | Some y => f y | _ => None end in

      match prog with
      | MSkip => Some ASkip

      | MSeq a b =>
        omap (convertProgram' a mapF nextFree tmp) (fun aprog =>
          omap (convertProgram' b mapF nextFree tmp) (fun bprog =>
            Some (ASeq aprog bprog)))

      | MAssign a =>
        match a with
        | MAVar x y => 
          match (mapF x, mapF y) with
          | (regM rx, regM ry) =>
            Some (AAssign (ARegReg rx ry))
          | (stackM sx, regM ry) =>
            Some (AAssign (AStackReg sx ry))
          | (regM rx, stackM sy) =>
            Some (AAssign (ARegStack rx sy))
          | (regM rx, constM cy) =>
            Some (AAssign (AConstInt rx cy))
          | _ => None
          end

        | MAConst x c =>
          match (mapF x) with
          | (regM rx) => Some (AAssign (AConstInt rx (iconst c)))
          | _ => None
          end

        | MAMem m x v i =>
          match (mapF x) with
          | (regM rx) => Some (AAssign (ARegMem rx (@mem _ m width_spec v) i))
          | _ => None
          end
        end

      | MOp o =>
        match o with
        | MIOpConst io a c =>
          match (mapF a) with
          | (regM ra) =>
            Some (AOp (IOpConst io ra (iconst c)))
          | _ => None
          end

        | MIOpReg io a b =>
          match (mapF a, mapF b) with
          | (regM ra, regM rb) =>
            Some (AOp (IOpReg io ra rb))
          | _ => None
          end

        | MCOpReg co a b =>
          match (mapF a, mapF b) with
          | (regM ra, regM rb) =>
            Some (AOp (COp co ra rb))
          | _ => None
          end

        | MDOpReg duo a b (Some x) =>
          match (mapF a, mapF b, mapF x) with
          | (regM ra, regM rb, regM rx) =>
            Some (AOp (DOp duo ra rb (Some rx)))
          | _ => None
          end

        | MDOpReg duo a b None =>
          match (mapF a, mapF b) with
          | (regM ra, regM rb) =>
            Some (AOp (DOp duo ra rb None))
          | _ => None
          end

        | MOpRot ro a c =>
          match (mapF a) with
          | (regM ra) =>
            Some (AOp (ROp ro ra c))
          | _ => None
          end
        end

      | MCond c a b =>

        let conv := (fun x => convertProgram' x mapF nextFree tmp) in

        omap (conv a) (fun aprog => omap (conv b) (fun bprog =>
          match c with
          | MCReg t x y =>
            match (mapF x, mapF y) with
            | (regM r0, regM r1) =>
              Some (ACond (CReg _ t r0 r1) aprog bprog)
            | _ => None
            end

          | MCConst t x y =>
            match (mapF x) with
            | (regM r) =>
              Some (ACond (CConst _ t r (iconst y)) aprog bprog)
            | _ => None
            end

          | MZ x =>
            match (mapF x) with
            | (regM r) =>
              Some (ACond (CZero _ r) aprog bprog)
            | _ => None
            end
          end))

      | MFunexp e a =>
        let conv := (fun x => convertProgram' x mapF (S nextFree) tmp) in
        omap (conv a) (fun aprog =>
          match (mapF nextFree, mapF tmp) with
          | (regM rf, regM rt) =>

            Some (ASeq (ASeq
              (AAssign (AConstInt rf (iconst (natToWord _ O))))
              (AAssign (AConstInt rt (iconst (natToWord _ e)))))
              (AWhile (CReg _ TLt rf rt)
                (ASeq aprog (ASeq 
                (AOp (IOpConst Add rf (iconst (natToWord _ 1))))
                (AAssign (AConstInt rt (iconst (natToWord _ e))))))))
          | _ => None
          end)

      | MDef lbl f a =>
        omap (convertProgram' f mapF nextFree tmp) (fun fprog =>
          omap (convertProgram' a mapF (S nextFree) tmp) (fun aprog =>
            Some (ADef lbl fprog aprog)))
      | MCall lbl => Some (ACall lbl)
      end.

    (* TODO (rsloan): make these into parameters *)
    Definition convertProgram (prog: M.Program): option AlmostQhasm.Program :=
      convertProgram' prog tmpMap 100 100.

    Lemma convert_spec:  forall a a' b b' prog prog',
      convertProgram prog = Some prog' ->
      convertState a = Some a' -> convertState b = Some b' ->
      AlmostQhasm.evaluatesTo prog' a b <-> M.evaluatesTo prog a' b'.
    Admitted.

  End MedialConversion.
End PseudoMedialConversion.