Debian Codegraph @

src/Bedrock/Field/Translation/Expr.v

raw
1 From Coq Require Import ZArith.
2 From Coq Require Import List.
3 Require Import bedrock2.Syntax.
4 Require Import Crypto.Bedrock.Field.Common.Types.
5 Require Import Crypto.Language.API.
6 Require Import Crypto.Util.ZRange.
7 Local Open Scope Z_scope.
8
9 Import API.Compilers.
10 Import Types.Notations.
11
12 Section Expr.
13 Context
14 {width BW word mem locals ext_spec varname_gen error}
15 `{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}.
16 Context {ok : ok}.
17 Existing Instance Types.rep.Z.
18 Existing Instance Types.rep.listZ_local. (* local list representation *)
19 Definition max_range : zrange := {| lower := 0; upper := 2 ^ width - 1 |}.
20 Definition range_good (r : zrange) : bool := zrange_beq r max_range.
21
22 (* for the second argument of shifts *)
23 Definition width_range := r[0~>width-1]%zrange.
24
25 Local Notation Zcast r :=
26 (@expr.App
27 _ _ _ type_range (type.arrow type_Z type_Z)
28 (expr.Ident ident.Z_cast)
29 (expr.Ident (@ident.Literal base.type.zrange r))).
30 Local Notation Zcast2 r1 r2 :=
31 (@expr.App
32 _ _ _ type_range2 (type.arrow type_ZZ type_ZZ)
33 (expr.Ident ident.Z_cast2)
34 (@expr.App
35 _ _ _ type_range type_range2
36 (@expr.App
37 _ _ _ type_range (type.arrow type_range type_range2)
38 (expr.Ident ident.pair)
39 (expr.Ident (@ident.Literal base.type.zrange r1)))
40 (expr.Ident (@ident.Literal base.type.zrange r2)))).
41
42 (* Literal Zs or nats, and lists, do not need casts *)
43 Definition cast_exempt {var t} (e : @API.expr var t)
44 : bool :=
45 match e with
46 | (expr.Ident _ (ident.Literal base.type.Z z)) =>
47 true
48 | (expr.Ident _ (ident.Literal base.type.nat n)) =>
49 true
50 | expr.Var _ _ =>
51 true
52 | _ => false
53 end.
54
55 Definition invert_literal (x : Syntax.expr) : option Z :=
56 match x with
57 | expr.literal x => Some x
58 | _ => None
59 end.
60 Definition literal_eqb x y : bool :=
61 match invert_literal x with
62 | Some x => Z.eqb x y
63 | None => false
64 end.
65 Definition literal_ltwidth x :=
66 match invert_literal x with
67 | Some x => is_bounded_by_bool x width_range
68 | None => false
69 end.
70
71 Definition rshiftr
72 : rtype (type_Z -> type_Z -> type_Z) :=
73 fun x n =>
74 if literal_ltwidth n
75 then expr.op bopname.sru x n
76 else base_make_error _.
77
78 Definition rshiftl
79 : rtype (type_Z -> type_Z -> type_Z) :=
80 fun x n =>
81 if literal_ltwidth n
82 then expr.op bopname.slu x n
83 else base_make_error _.
84
85 Definition rtruncating_shiftl
86 : rtype (type_Z -> type_Z -> type_Z -> type_Z) :=
87 fun s x n =>
88 if literal_eqb s width
89 then if literal_ltwidth n
90 then expr.op bopname.slu x n
91 else make_error type_Z
92 else base_make_error _.
93
94 Definition rlnot_modulo
95 : rtype (type_Z -> type_Z -> type_Z) :=
96 fun x m =>
97 match invert_literal m with
98 | Some m => if (2^(Z.log2 m) =? m)
99 then expr.op bopname.xor
100 (if Z.log2 m =? width (* is this a good place to do this optimization? *)
101 then x
102 else expr.op bopname.and x (expr.literal (Z.ones (Z.log2 m))))
103 (expr.literal (Z.ones (Z.log2 m)))
104 else make_error type_Z
105 | None => make_error type_Z
106 end.
107
108 Definition rselect
109 : rtype (type_Z -> type_Z -> type_Z -> type_Z) :=
110 fun c x y =>
111 if literal_eqb x 0
112 then if literal_eqb y (2^width - 1)
113 then expr.op bopname.add (expr.op1 op1.opp (expr.literal 1))
114 (expr.op bopname.eq c (expr.literal 0))
115 else base_make_error _
116 else base_make_error _.
117
118 Definition rnth_default
119 : rtype (type_Z -> type_listZ -> type_nat -> type_Z) :=
120 fun d l i =>
121 match i with
122 | expr.literal i =>
123 nth_default d l (Z.to_nat i)
124 | _ => base_make_error _
125 end.
126
127 Definition rmul_high
128 : rtype (type_Z -> type_Z -> type_Z -> type_Z) :=
129 fun s x y =>
130 if literal_eqb s (2 ^ width)
131 then expr.op bopname.mulhuu x y
132 else base_make_error _.
133
134 Definition is_cast_literal_ident {t} (i : ident.ident t) : bool :=
135 match i with
136 | ident.Literal base.type.zrange r =>
137 range_good r
138 | _ => false
139 end.
140 Definition is_cast_literal
141 {var t} (e : @API.expr var t) : bool :=
142 match e with
143 | expr.Ident type_range i =>
144 is_cast_literal_ident i
145 | _ => false
146 end.
147
148 Definition is_pair_range {t} (i : ident.ident t) : bool :=
149 match i with
150 | ident.pair base_range base_range => true
151 | _ => false
152 end.
153 Definition is_cast2_literal_App2
154 {var t} (e : @API.expr var t) : bool :=
155 match e with
156 | expr.Ident (type.arrow type_range
157 (type.arrow type_range type_range2))
158 i =>
159 is_pair_range i
160 | _ => false
161 end.
162 Definition is_cast2_literal_App1
163 {var t} (e : @API.expr var t) : bool :=
164 match e with
165 | expr.App
166 type_range (type.arrow type_range type_range2)
167 f r1 =>
168 is_cast2_literal_App2 f && is_cast_literal r1
169 | _ => false
170 end.
171 Definition is_cast2_literal
172 {var t} (e : @API.expr var t) : bool :=
173 match e with
174 | expr.App type_range type_range2 f r2 =>
175 is_cast2_literal_App1 f && is_cast_literal r2
176 | _ => false
177 end.
178
179 Definition is_cast_ident {t} (i : ident.ident t) : bool :=
180 match i with
181 | ident.Z_cast => true
182 | ident.Z_cast2 => true
183 | _ => false
184 end.
185
186 Definition is_cast_ident_expr {var t}
187 (e : @API.expr var t) : bool :=
188 match e with
189 | expr.Ident _ i => is_cast_ident i
190 | _ => false
191 end.
192
193 Definition is_cast
194 {var t} (e : @API.expr var t) : bool :=
195 match e with
196 | expr.App type_range (type.arrow type_Z type_Z) f r =>
197 is_cast_ident_expr f && is_cast_literal r
198 | expr.App type_range2 (type.arrow type_ZZ type_ZZ) f r =>
199 is_cast_ident_expr f && is_cast2_literal r
200 | _ => false
201 end.
202
203 (* only require cast for the argument of (App f x) if:
204 - f is not a cast
205 - f is not mul_high (then, x = 2^width)
206 - f is not (lnot_modulo _) (then x is allowed to be 2^width)
207 - f is not (nth_default ?d ?l) (i doesn't need to fit in a word) *)
208 Definition require_cast_for_arg
209 {var t} (e : @API.expr var t) : bool :=
210 match e with
211 | Zcast r => negb (range_good r)
212 | Zcast2 r1 r2 =>
213 negb (range_good r1 && range_good r2)
214 | expr.Ident _ ident.Z_mul_high => false
215 | expr.App
216 _ _ (expr.Ident _ ident.Z_lnot_modulo)
217 _ => false
218 | expr.App
219 _ _ (expr.App
220 _ _ (expr.Ident _ (ident.List_nth_default _))
221 _) _ => false
222 | _ => true
223 end.
224
225 Definition translate_binop {t} (i : ident.ident t)
226 : option (rtype t) :=
227 match i with
228 | ident.Z_add => Some (expr.op bopname.add)
229 | ident.Z_sub => Some (expr.op bopname.sub)
230 | ident.Z_mul => Some (expr.op bopname.mul)
231 | ident.Z_ltz => Some (expr.op bopname.ltu)
232 | ident.Z_lor => Some (expr.op bopname.or)
233 | ident.Z_land => Some (expr.op bopname.and)
234 | ident.Z_lxor => Some (expr.op bopname.xor)
235 | _ => None
236 end.
237
238 Definition translate_ident
239 {t} (i : ident.ident t) : rtype t :=
240 match i in ident.ident t0 return rtype t0 with
241 | ident.fst _ _ => fst
242 | ident.snd _ _ => snd
243 | ident.Z_opp => fun x => expr.op bopname.sub (expr.literal 0) x
244 | ident.List_nth_default base_Z => rnth_default
245 | ident.Z_shiftr => rshiftr
246 | ident.Z_shiftl => rshiftl
247 | ident.Z_truncating_shiftl => rtruncating_shiftl
248 | ident.Z_lnot_modulo => rlnot_modulo
249 | ident.Z_zselect => rselect
250 | ident.Z_mul_high => rmul_high
251 | ident.Z_cast => fun _ x => x
252 | ident.Z_cast2 => fun _ x => x
253 | i => match translate_binop i with
254 | Some x => x
255 | None => make_error _
256 end
257 end.
258
259 Definition translate_cast_exempt {t}
260 (require_in_bounds : bool)
261 (e : @API.expr ltype t) : rtype t :=
262 match e in expr.expr t0 return rtype t0 with
263 | (expr.Ident type_Z (ident.Literal base.type.Z z)) =>
264 if require_in_bounds
265 then if is_bounded_by_bool z max_range
266 then expr.literal z
267 else make_error _
268 else expr.literal z
269 | (expr.Ident type_nat (ident.Literal base.type.nat n)) =>
270 expr.literal (Z.of_nat n)
271 | expr.Var type_listZ x => map expr.var x
272 | expr.Var type_Z x => expr.var x
273 | _ => make_error _
274 end.
275
276 Fixpoint translate_expr
277 (require_cast : bool)
278 {t} (e : @API.expr ltype t) : rtype t :=
279 if cast_exempt e
280 then translate_cast_exempt require_cast e
281 else if require_cast
282 then
283 match e in expr.expr t0 return rtype t0 with
284 | expr.App _ _ f x =>
285 if is_cast f
286 then translate_expr false f (translate_expr false x)
287 else make_error _
288 | _ => make_error _
289 end
290 else
291 match e in expr.expr t0 return rtype t0 with
292 | expr.App _ _ f x =>
293 let rc := require_cast_for_arg f in
294 translate_expr false f (translate_expr rc x)
295 | expr.Ident _ i => translate_ident i
296 | expr.Var _ x => rtype_of_ltype _ x
297 | _ => make_error _
298 end.
299 End Expr.