Debian Codegraph @

src/Bedrock/Field/Stringification/FlattenVarData.v

raw
1 (* This file is a duplicate of Flatten.v, but talks about var_data instead of ltype *)
2 From Coq Require Import String.
3 From Coq Require Import List.
4 From Coq Require Import String.
5 Require Import Crypto.Stringification.Language.
6 Require Crypto.Stringification.C.
7 Require Crypto.Stringification.IR.
8 Require Import Crypto.Language.API.
9 Require Import Crypto.Bedrock.Field.Common.Types.
10 Import ListNotations. Local Open Scope Z_scope.
11
12 Import API.Compilers.
13 Import Bedrock.Field.Common.Types.Notations.
14 Import Language.Compilers.ToString Language.Compilers.ToString.OfPHOAS.
15 Import C.Compilers.ToString IR.Compilers.ToString.
16
17 (* For argument and return variable names, fiat-crypto expects a nested
18 structure while bedrock2 expects flat lists; this file contains mechanical
19 conversions between the two. *)
20
21 (** TODO: MOVE ME (WHERE?) *)
22 Notation name_with_type := (string * option int.type)%type (only parsing).
23 Definition print_name (n : name_with_type) : string := fst n.
24 Definition print_type (n : name_with_type) : string
25 := let _ := Compilers.Options.default_language_naming_conventions in
26 C.String.type.primitive.to_string "" IR.type.Z (snd n).
27 Definition print_ptr_type (n : name_with_type) : string
28 := let _ := Compilers.Options.default_language_naming_conventions in
29 C.String.type.primitive.to_string "" IR.type.Zptr (snd n).
30 Definition print_cast (n : name_with_type) : string
31 := ("(" ++ print_type n ++ ")")%string.
32 Definition print_ptr_cast (n : name_with_type) : string
33 := ("(" ++ print_ptr_type n ++ ")")%string.
34
35 Section Flatten.
36 Fixpoint flatten_base_var_data {t}
37 : base_var_data t -> list name_with_type :=
38 match t return base_var_data t -> _ with
39 | base.type.prod a b =>
40 fun x : base_var_data a * base_var_data b =>
41 (flatten_base_var_data (fst x) ++ flatten_base_var_data (snd x))
42 | base.type.list _ => fun '(n, ty, len, _typedef) => [(n, ty)]
43 | base_Z => fun '(n, is_ptr, ty, _typedef) => [(n, ty)]
44 | _ => fun _ => []
45 end.
46
47 Fixpoint flatten_argnames_of_var_data {t}
48 : type.for_each_lhs_of_arrow var_data t -> list name_with_type :=
49 match t with
50 | type.base b => fun _ : unit => []
51 | type.arrow (type.base a) b =>
52 fun x : base_var_data a * _ =>
53 flatten_base_var_data (fst x) ++ flatten_argnames_of_var_data (snd x)
54 | _ => fun _ => [] (* garbage; function arguments not allowed *)
55 end.
56
57 Definition flatten_retnames_of_var_data {t}
58 : base_var_data t -> list name_with_type :=
59 flatten_base_var_data.
60
61 Fixpoint flatten_listonly_base_var_data {t}
62 : base_listonly (base_var_data base_listZ) t -> list name_with_type :=
63 match t with
64 | base.type.prod a b =>
65 fun x =>
66 (flatten_listonly_base_var_data (fst x))
67 ++ flatten_listonly_base_var_data (snd x)
68 | base_listZ => flatten_base_var_data
69 | _ => fun _ => []
70 end.
71
72 Fixpoint flatten_listexcl_base_var_data {t}
73 : base_listexcl base_var_data t -> list name_with_type :=
74 match t with
75 | base.type.prod a b =>
76 fun x =>
77 (flatten_listexcl_base_var_data (fst x))
78 ++ flatten_listexcl_base_var_data (snd x)
79 | base_listZ => fun _ => []
80 | _ => flatten_base_var_data
81 end.
82 End Flatten.