Debian Codegraph @

src/Bedrock/Field/Translation/LoadStoreList.v

raw
1 (* IF YOU CHANGE THIS FILE YOU MUST ALSO CHANGE src/Bedrock/Field/Stringification/LoadStoreListVarData.v ! *)
2 From Coq Require Import String.
3 From Coq Require Import List.
4 From Coq Require Import Lia.
5 From Coq Require Import ZArith.
6 Require Import bedrock2.Array.
7 Require Import bedrock2.Scalars.
8 Require Import bedrock2.Syntax.
9 Require Import bedrock2.WeakestPreconditionProperties.
10 Require Import coqutil.Word.Interface.
11 Require Import Crypto.Bedrock.Field.Common.Types.
12 Require Import Crypto.Language.API.
13 Require Import Crypto.Util.ListUtil.
14 Require Import Crypto.Util.ZRange.
15 Import ListNotations. Local Open Scope Z_scope.
16
17 Import API.Compilers.
18 Import Types.Notations.
19
20 (* For proofs, it's ideal to assume that at the "cmd" level of abstraction, the
21 memory doesn't change. This is actually true for fiat-crypto functions, which
22 follow a pattern in which they read all their from input lists, then only deal
23 with local variables until they store their results in output lists.
24
25 Therefore, at the "func" level of abstraction, we first read the data from
26 memory and then pass the local variables to the "cmd" level. The "cmd" level
27 then returns local variables which we store. This file handles the
28 loading/storing part of that process. *)
29 Section Lists.
30 Context
31 {width BW word mem locals ext_spec varname_gen error}
32 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
33 Local Existing Instance rep.Z.
34
35 Fixpoint extract_listnames {t}
36 : base_ltype (listZ:=rep.listZ_mem) t ->
37 listonly_base_ltype t * listexcl_base_ltype t
38 :=
39 match t as t0 return
40 base_ltype t0 ->
41 listonly_base_ltype t0
42 * listexcl_base_ltype t0 with
43 | base.type.prod a b =>
44 fun x =>
45 let p1 := extract_listnames (fst x) in
46 let p2 := extract_listnames (snd x) in
47 ((fst p1, fst p2), (snd p1, snd p2))
48 | base_listZ => fun x => (x, tt)
49 | _ => fun x => (tt, x)
50 end.
51
52 Definition load_list_item
53 (load_size : access_size)
54 (start : Syntax.expr.expr) (i : nat)
55 : Syntax.expr.expr :=
56 let nbytes := Memory.bytes_per (width:=width) load_size in
57 let offset := expr.literal (Z.of_nat (nbytes * i)) in
58 let loc := expr.op bopname.add start offset in
59 expr.load load_size loc.
60
61 Fixpoint load_list
62 (load_size : access_size)
63 (start : Syntax.expr.expr) (i rem nextn : nat)
64 : nat * list string * Syntax.cmd.cmd :=
65 match rem with
66 | O => (0%nat, [], cmd.skip)
67 | S rem' =>
68 let rec := load_list load_size start (S i) rem' (S nextn) in
69 (S (fst (fst rec)), (varname_gen nextn) :: snd (fst rec),
70 cmd.seq (cmd.set (varname_gen nextn)
71 (load_list_item load_size start i))
72 (snd rec))
73 end.
74
75 Fixpoint load_all_lists {t : base.type} (nextn : nat)
76 : base_ltype (listZ:=rep.listZ_mem) t ->
77 base_listonly nat t ->
78 base_access_sizes (listZ:=rep.listZ_mem) t ->
79 nat * base_ltype (listZ:=rep.listZ_local) t * cmd.cmd :=
80 match t with
81 | base.type.prod a b =>
82 fun x l s =>
83 let load1 := load_all_lists nextn (fst x) (fst l) (fst s) in
84 let nextn1 := (nextn + fst (fst load1))%nat in
85 let load2 := load_all_lists nextn1 (snd x) (snd l) (snd s) in
86 let nvars := (fst (fst load1) + fst (fst load2))%nat in
87 (nvars, (snd (fst load1), snd (fst load2)),
88 cmd.seq (snd load1) (snd load2))
89 | base_listZ =>
90 fun (x : string) (l : nat) (s : access_size) =>
91 load_list s (expr.var x) 0 l nextn
92 | _ =>
93 fun (x : string) (l s : unit) => (0%nat, x, cmd.skip)
94 end.
95
96 (* read lists from arguments; changes the type system from in-memory
97 lists to local lists *)
98 Fixpoint load_arguments {t} (nextn : nat)
99 : type.for_each_lhs_of_arrow (ltype (listZ:=rep.listZ_mem)) t ->
100 type.for_each_lhs_of_arrow list_lengths t ->
101 type.for_each_lhs_of_arrow access_sizes t ->
102 nat (* number of fresh variable names used *)
103 * type.for_each_lhs_of_arrow (ltype (listZ:=rep.listZ_local)) t
104 * cmd.cmd :=
105 match t as t0 return
106 type.for_each_lhs_of_arrow _ t0 ->
107 type.for_each_lhs_of_arrow _ t0 ->
108 type.for_each_lhs_of_arrow _ t0 ->
109 _ * type.for_each_lhs_of_arrow _ t0 * _
110 with
111 | type.base b =>
112 fun (args bounds sizes : unit) => (0%nat, args, cmd.skip)
113 | type.arrow (type.base s) d =>
114 fun (args : base_ltype s * type.for_each_lhs_of_arrow _ d)
115 (llengths : base_listonly nat s * type.for_each_lhs_of_arrow _ d)
116 (sizes : base_access_sizes s
117 * type.for_each_lhs_of_arrow _ d) =>
118 let load_fst :=
119 load_all_lists nextn (fst args) (fst llengths) (fst sizes) in
120 let nextn' := (nextn + fst (fst load_fst))%nat in
121 let load_snd :=
122 load_arguments nextn' (snd args) (snd llengths) (snd sizes) in
123 let nvars := (fst (fst load_fst) + fst (fst load_snd))%nat in
124 let args' := (snd (fst load_fst), snd (fst load_snd)) in
125 let cmd := cmd.seq (snd load_fst) (snd load_snd) in
126 (nvars, args', cmd)
127 | type.arrow _ d =>
128 (* no arrow arguments allowed; insert unit type *)
129 fun args bounds sizes =>
130 let load_snd :=
131 load_arguments nextn (snd args) (snd bounds) (snd sizes) in
132 (fst (fst load_snd), (tt, snd (fst load_snd)), snd load_snd)
133 end.
134
135 Definition store_list_item
136 (size : access_size)
137 (start value : Syntax.expr.expr) (i : nat)
138 : cmd.cmd :=
139 let nbytes := Memory.bytes_per (width:=width) size in
140 let offset := expr.literal (Z.of_nat (nbytes * i)) in
141 let loc := expr.op bopname.add start offset in
142 cmd.store size loc value.
143
144 Fixpoint store_list
145 (size : access_size)
146 (start : Syntax.expr.expr)
147 (values : list Syntax.expr.expr)
148 (i : nat)
149 : cmd.cmd :=
150 match values with
151 | [] => cmd.skip
152 | v :: values' =>
153 cmd.seq (store_list_item size start v i)
154 (store_list size start values' (S i))
155 end.
156
157 Fixpoint store_return_values {t : base.type}
158 : base_ltype (listZ:=rep.listZ_local) t ->
159 base_ltype (listZ:=rep.listZ_mem) t ->
160 access_sizes (listZ:=rep.listZ_mem) (type.base t) ->
161 list_lengths (type.base t) * cmd.cmd :=
162 match t as t0 return
163 base_ltype t0 -> base_ltype t0 -> access_sizes (type.base t0) ->
164 list_lengths (type.base t0) * _ with
165 | base.type.prod a b =>
166 fun x y s =>
167 let a := store_return_values (fst x) (fst y) (fst s) in
168 let b := store_return_values (snd x) (snd y) (snd s) in
169 ((fst a, fst b), cmd.seq (snd a) (snd b))
170 | base_listZ =>
171 (* store list at location pointed to by y *)
172 fun (x : list string) (y : string) (s : access_size) =>
173 (length x, store_list s (expr.var y) (map expr.var x) 0)
174 | _ =>
175 (* rename variable *)
176 fun (x y : string) _ => (tt, cmd.set y (expr.var x))
177 end.
178 End Lists.