| 1 |
(* IF YOU CHANGE THIS FILE YOU MUST ALSO CHANGE src/Bedrock/Field/Stringification/FlattenVarData.v ! *) |
| 2 |
From Coq Require Import String. |
| 3 |
From Coq Require Import List. |
| 4 |
Require Import bedrock2.Array. |
| 5 |
Require Import bedrock2.Syntax. |
| 6 |
Require Import bedrock2.Semantics. |
| 7 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 8 |
Require Import Crypto.Language.API. |
| 9 |
Import ListNotations. Local Open Scope Z_scope. |
| 10 |
|
| 11 |
Import API.Compilers. |
| 12 |
Import Types.Notations. |
| 13 |
|
| 14 |
(* For argument and return variable names, fiat-crypto expects a nested |
| 15 |
structure while bedrock2 expects flat lists; this file contains mechanical |
| 16 |
conversions between the two. *) |
| 17 |
|
| 18 |
Section Flatten. |
| 19 |
Context |
| 20 |
{width BW word mem locals ext_spec varname_gen error} |
| 21 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 22 |
(* these conversions should happen before loading arguments and after |
| 23 |
storing return values, so they use in-memory lists *) |
| 24 |
Local Existing Instance rep.listZ_mem. |
| 25 |
Local Existing Instance rep.Z. |
| 26 |
|
| 27 |
Fixpoint flatten_base_ltype {t} |
| 28 |
: base_ltype t -> list string := |
| 29 |
match t with |
| 30 |
| base.type.prod a b => |
| 31 |
fun x : base_ltype a * base_ltype b => |
| 32 |
(flatten_base_ltype (fst x) ++ flatten_base_ltype (snd x)) |
| 33 |
| base_listZ => fun x : string => [x] |
| 34 |
| _ => fun x : string => [x] |
| 35 |
end. |
| 36 |
|
| 37 |
Fixpoint flatten_base_rtype {t} |
| 38 |
: base_rtype t -> list Syntax.expr := |
| 39 |
match t as t0 return base_rtype t0 -> _ with |
| 40 |
| base.type.prod a b => |
| 41 |
fun x : base_rtype a * base_rtype b => |
| 42 |
(flatten_base_rtype (fst x) ++ flatten_base_rtype (snd x)) |
| 43 |
| base_listZ => fun x : Syntax.expr => [x] |
| 44 |
| _ => fun x : Syntax.expr => [x] |
| 45 |
end. |
| 46 |
|
| 47 |
Fixpoint flatten_argnames {t} |
| 48 |
: type.for_each_lhs_of_arrow ltype t -> list string := |
| 49 |
match t with |
| 50 |
| type.base b => fun _ : unit => [] |
| 51 |
| type.arrow (type.base a) b => |
| 52 |
fun x : base_ltype a * _ => |
| 53 |
flatten_base_ltype (fst x) ++ flatten_argnames (snd x) |
| 54 |
| _ => fun _ => [] (* garbage; function arguments not allowed *) |
| 55 |
end. |
| 56 |
|
| 57 |
Fixpoint flatten_args {t} |
| 58 |
: type.for_each_lhs_of_arrow rtype t -> list Syntax.expr := |
| 59 |
match t as t0 return type.for_each_lhs_of_arrow rtype t0 -> _ with |
| 60 |
| type.base b => fun _ => [] |
| 61 |
| type.arrow (type.base a) b => |
| 62 |
fun x : base_rtype a * _ => |
| 63 |
flatten_base_rtype (fst x) ++ flatten_args (snd x) |
| 64 |
| _ => fun _ => [] (* garbage; function arguments not allowed *) |
| 65 |
end. |
| 66 |
|
| 67 |
Definition flatten_retnames {t} |
| 68 |
: base_ltype t -> list string := |
| 69 |
flatten_base_ltype. |
| 70 |
|
| 71 |
Definition flatten_rets {t} |
| 72 |
: base_rtype t -> list Syntax.expr := |
| 73 |
flatten_base_rtype. |
| 74 |
|
| 75 |
Fixpoint flatten_listonly_base_ltype {t} |
| 76 |
: listonly_base_ltype t -> list string := |
| 77 |
match t with |
| 78 |
| base.type.prod a b => |
| 79 |
fun x => |
| 80 |
(flatten_listonly_base_ltype (fst x)) |
| 81 |
++ flatten_listonly_base_ltype (snd x) |
| 82 |
| base_listZ => fun x => [x] |
| 83 |
| _ => fun _ => [] |
| 84 |
end. |
| 85 |
|
| 86 |
Fixpoint flatten_listexcl_base_ltype {t} |
| 87 |
: listexcl_base_ltype t -> list string := |
| 88 |
match t with |
| 89 |
| base.type.prod a b => |
| 90 |
fun x => |
| 91 |
(flatten_listexcl_base_ltype (fst x)) |
| 92 |
++ flatten_listexcl_base_ltype (snd x) |
| 93 |
| base_listZ => fun _ => [] |
| 94 |
| _ => fun x => [x] |
| 95 |
end. |
| 96 |
End Flatten. |