Debian Codegraph @

src/Bedrock/Field/Translation/Cmd.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 From Coq Require Import String.
4 Require Import Crypto.Bedrock.Field.Common.Types.
5 Require Import Crypto.Bedrock.Field.Translation.Expr.
6 Require Import Crypto.Language.API.
7 Require Import Crypto.Util.Option.
8 Require Import Crypto.Util.Notations.
9 Import ListNotations. Local Open Scope Z_scope.
10
11 (* for [eauto with lia] *)
12 Require Import Crypto.BoundsPipeline.
13
14 Import API.Compilers.
15 Import Types.Notations.
16
17 Section Cmd.
18 Context
19 {width BW word mem locals ext_spec varname_gen error}
20 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
21 Existing Instance Types.rep.Z.
22 Existing Instance Types.rep.listZ_local. (* local list representation *)
23
24 Fixpoint assign_list (nextn : nat) (xs : base_rtype base_listZ)
25 : nat * base_ltype base_listZ * Syntax.cmd.cmd :=
26 match xs with
27 | [] => (0%nat, [], Syntax.cmd.skip)
28 | x :: xs' =>
29 let rec := assign_list (S nextn) xs' in
30 (S (fst (fst rec)), varname_gen nextn :: snd (fst rec),
31 Syntax.cmd.seq (Syntax.cmd.set (varname_gen nextn) x) (snd rec))
32 end.
33
34 Fixpoint assign_base {t : base.type} (nextn : nat)
35 : base_rtype t -> (nat * base_ltype t * Syntax.cmd.cmd) :=
36 match t with
37 | base.type.prod a b =>
38 fun rhs =>
39 let assign1 := assign_base nextn (fst rhs) in
40 let assign2 := assign_base (nextn + fst (fst assign1)) (snd rhs) in
41 ((fst (fst assign1) + fst (fst assign2))%nat,
42 (snd (fst assign1), snd (fst assign2)),
43 Syntax.cmd.seq (snd assign1) (snd assign2))
44 | base.type.list (base.type.type_base base.type.Z) =>
45 assign_list nextn
46 | base.type.list _ | base.type.option _ | base.type.unit
47 | base.type.type_base _ =>
48 fun rhs =>
49 let v := varname_gen nextn in
50 (1%nat, v, Syntax.cmd.set v rhs)
51 end.
52
53 Definition assign {t} (nextn : nat)
54 : rtype t -> (nat * ltype t * Syntax.cmd.cmd) :=
55 match t as t0 return
56 rtype t0 -> nat * ltype t0 * _ with
57 | type.base b => assign_base (t:=b) nextn
58 | _ =>
59 fun _ =>
60 (0%nat, dummy_ltype _, Syntax.cmd.skip)
61 end.
62
63 Definition translate_ident2_for_cmd {a b c} (i : ident (a -> b -> c))
64 : ltype a -> ltype b -> option (ltype c)
65 := match i in ident t return ltype (type.domain t) -> ltype (type.domain (type.codomain t)) -> option (ltype (type.codomain (type.codomain t))) with
66 | ident.cons base_Z => fun x xs => Some (x :: xs)
67 | ident.pair _ _ => fun x y => Some (x, y)
68 | _ => fun _ _ => None
69 end.
70
71 Fixpoint translate_cmd
72 {t} (e : @API.expr ltype t) (nextn : nat)
73 : nat (* number of variable names used *)
74 * ltype t (* variables in which return values are stored *)
75 * Syntax.cmd.cmd (* actual program *) :=
76 match e in expr.expr t0
77 return (nat * ltype t0 * Syntax.cmd.cmd) with
78 | expr.LetIn (type.base t1) (type.base t2) x f =>
79 let trx := assign nextn (translate_expr true x) in
80 let trf := translate_cmd (f (snd (fst trx))) (nextn + fst (fst trx)) in
81 ((fst (fst trx) + fst (fst trf))%nat,
82 snd (fst trf),
83 Syntax.cmd.seq (snd trx) (snd trf))
84 | expr.App _ _ _ _ as e
85 => let result_if_ident2
86 := (ixy <- invert_expr.invert_AppIdent2_cps e (@translate_cmd) (@translate_cmd);
87 let '(existT _ (i, translate_cmd_x, translate_cmd_y)) := ixy in
88 let trx := translate_cmd_x nextn in
89 let try := translate_cmd_y (fst (fst trx) + nextn)%nat in
90 vars <- translate_ident2_for_cmd i (snd (fst trx)) (snd (fst try));
91 Some ((fst (fst trx) + fst (fst try))%nat,
92 vars,
93 Syntax.cmd.seq (snd trx) (snd try)))%option in
94 match result_if_ident2 with
95 | Some res => res
96 | None =>
97 let v := translate_expr true e in
98 assign nextn v
99 end
100 | expr.Ident type_listZ (ident.nil _) =>
101 (0%nat, [], Syntax.cmd.skip)
102 | expr.Ident _ i =>
103 let v := translate_expr true (expr.Ident i) in
104 assign nextn v
105 | expr.Var _ v =>
106 let v := translate_expr true (expr.Var v) in
107 assign nextn v
108 | _ => (0%nat, dummy_ltype _, Syntax.cmd.skip)
109 end.
110 End Cmd.