| 1 |
From Coq Require Import ZArith. |
| 2 |
From Coq Require Import List. |
| 3 |
From Coq Require Import String. |
| 4 |
Require Import bedrock2.Syntax. |
| 5 |
Require Import bedrock2.ToCString. |
| 6 |
Require Import coqutil.Word.Naive coqutil.Map.SortedListWord coqutil.Map.SortedListString. |
| 7 |
Require Import Crypto.Stringification.Language. |
| 8 |
Require Import Crypto.Stringification.IR. |
| 9 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 10 |
Require Import Crypto.Bedrock.Field.Common.Arrays.MakeAccessSizes. |
| 11 |
Require Import Crypto.Bedrock.Field.Common.Arrays.MakeListLengths. |
| 12 |
Require Import Crypto.Bedrock.Field.Common.Names.MakeNames. |
| 13 |
Require Import Crypto.Bedrock.Field.Common.Names.VarnameGenerator. |
| 14 |
Require Import Crypto.Bedrock.Field.Translation.Parameters.Defaults. |
| 15 |
Require Import Crypto.Bedrock.Field.Translation.Func. |
| 16 |
Require Import Crypto.Bedrock.Field.Stringification.FlattenVarData. |
| 17 |
Require Import Crypto.Bedrock.Field.Stringification.LoadStoreListVarData. |
| 18 |
Require Import Crypto.Stringification.C. |
| 19 |
Require Import Crypto.Language.API. |
| 20 |
Require Import Crypto.Util.Option. |
| 21 |
Import Stringification.Language.Compilers. |
| 22 |
Import Stringification.IR.Compilers.ToString. |
| 23 |
Import Stringification.C.Compilers.ToString. |
| 24 |
|
| 25 |
Import ListNotations Types.Notations. |
| 26 |
Import ToString.OfPHOAS. |
| 27 |
Local Open Scope string_scope. |
| 28 |
Local Open Scope list_scope. |
| 29 |
|
| 30 |
Section with_parameters. |
| 31 |
Context |
| 32 |
{width BW word mem locals ext_spec varname_gen error} |
| 33 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 34 |
|
| 35 |
Fixpoint make_base_var_data {t} |
| 36 |
: base_ltype t -> list_lengths (type.base t) -> |
| 37 |
option (base_var_data t) := |
| 38 |
let int_type := |
| 39 |
ToString.int.unsigned (Z.to_nat width) in |
| 40 |
match t as t0 return |
| 41 |
base_ltype t0 -> |
| 42 |
list_lengths (type.base t0) -> |
| 43 |
option (base_var_data t0) with |
| 44 |
| base.type.prod A B => |
| 45 |
fun x ll => |
| 46 |
(x1 <- make_base_var_data (fst x) (fst ll); |
| 47 |
x2 <- make_base_var_data (snd x) (snd ll); |
| 48 |
Some (x1, x2))%option |
| 49 |
| base_Z => |
| 50 |
fun name _ => |
| 51 |
let is_pointer := false in (* never a pointer *) |
| 52 |
let typedef := None in (* no typedefs for bedrock2 *) |
| 53 |
Some (name, is_pointer, Some int_type, typedef) |
| 54 |
| base_listZ => |
| 55 |
fun name len => |
| 56 |
let typedef := None in (* no typedefs for bedrock2 *) |
| 57 |
Some (name, Some int_type, len, typedef) |
| 58 |
| _ => fun _ _ => None |
| 59 |
end. |
| 60 |
|
| 61 |
Definition make_var_data {t} |
| 62 |
: ltype t -> list_lengths t -> option (var_data t) := |
| 63 |
match t with |
| 64 |
| type.base b => make_base_var_data |
| 65 |
| type.arrow _ _ => fun _ _ => None |
| 66 |
end. |
| 67 |
|
| 68 |
Fixpoint make_var_data_of_args {t} |
| 69 |
: type.for_each_lhs_of_arrow ltype t -> |
| 70 |
type.for_each_lhs_of_arrow list_lengths t -> |
| 71 |
option (type.for_each_lhs_of_arrow var_data t) := |
| 72 |
match t as t0 return |
| 73 |
type.for_each_lhs_of_arrow ltype t0 -> |
| 74 |
type.for_each_lhs_of_arrow list_lengths t0 -> |
| 75 |
option (type.for_each_lhs_of_arrow var_data t0) |
| 76 |
with |
| 77 |
| type.arrow s d => |
| 78 |
fun names lengths => |
| 79 |
(x <- make_var_data (fst names) (fst lengths); |
| 80 |
y <- make_var_data_of_args (snd names) (snd lengths); |
| 81 |
Some (x, y))%option |
| 82 |
| type.base b => fun _ _ => Some tt |
| 83 |
end. |
| 84 |
|
| 85 |
Definition make_header |
| 86 |
{skip_typedefs : skip_typedefs_opt} |
| 87 |
{t} |
| 88 |
(innames : type.for_each_lhs_of_arrow ltype t) |
| 89 |
(inlengths : type.for_each_lhs_of_arrow list_lengths t) |
| 90 |
(inbounds : type.for_each_lhs_of_arrow |
| 91 |
ZRange.type.option.interp t) |
| 92 |
(outnames : ltype (type.base (type.final_codomain t))) |
| 93 |
(outlengths : list_lengths (type.base (type.final_codomain t))) |
| 94 |
(outbounds : ZRange.type.option.interp |
| 95 |
(type.base (type.final_codomain t))) |
| 96 |
: option (list string) := |
| 97 |
match make_var_data_of_args innames inlengths, |
| 98 |
make_base_var_data outnames outlengths with |
| 99 |
| Some indata, Some outdata => |
| 100 |
Some (["/*"] |
| 101 |
++ [" * Input Bounds:"] |
| 102 |
++ (List.map (fun v => " * " ++ v)%string |
| 103 |
(input_bounds_to_string indata inbounds)) |
| 104 |
++ [" * Output Bounds:"] |
| 105 |
++ (List.map (fun v => " * " ++ v)%string |
| 106 |
(bound_to_string outdata outbounds)) |
| 107 |
++ [" */"]) |
| 108 |
| _, _ => None |
| 109 |
end. |
| 110 |
End with_parameters. |
| 111 |
|
| 112 |
Definition bedrock_func_to_lines (f : string * func) |
| 113 |
: list string := |
| 114 |
[c_func f]. |
| 115 |
|
| 116 |
Definition wrap_call |
| 117 |
{width BW word mem locals ext_spec varname_gen error} |
| 118 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error} |
| 119 |
{t} |
| 120 |
(indata : type.for_each_lhs_of_arrow var_data t) |
| 121 |
(outdata : base_var_data (type.final_codomain t)) |
| 122 |
(f : string*func) |
| 123 |
(insizes : type.for_each_lhs_of_arrow access_sizes t) |
| 124 |
(outsizes : base_access_sizes (type.final_codomain t)) |
| 125 |
: string |
| 126 |
:= let part := extract_list_var_data outdata in |
| 127 |
let out_ptrs := flatten_listonly_base_var_data (fst part) in |
| 128 |
let innames := out_ptrs ++ flatten_argnames_of_var_data indata in |
| 129 |
let outnames := flatten_listexcl_base_var_data (snd part) in |
| 130 |
let '(name, (args, rets, _body)) := f in |
| 131 |
(* slightly modified from https://github.com/mit-plv/bedrock2/blob/13365e8113131601a60dd6b6ffddc5a0b92aed58/bedrock2/src/bedrock2/ToCString.v#L151-L154 *) |
| 132 |
let '(precall, all_args) |
| 133 |
:= match rets, outnames with |
| 134 |
| nil, _ | _, nil => (name, outnames ++ innames) |
| 135 |
| cons _ _, cons r0 _ |
| 136 |
=> let r0 := List.last outnames r0 in |
| 137 |
let outnames' := List.removelast outnames in |
| 138 |
(("*" ++ print_name r0 ++ " = " ++ print_cast r0 ++ name)%string, outnames' ++ innames) |
| 139 |
end%list in |
| 140 |
((precall ++ "(") |
| 141 |
++ (String.concat |
| 142 |
", " |
| 143 |
(List.map |
| 144 |
(** TODO: Is there a better way to do this, e.g., pulling from insizes? *) |
| 145 |
(fun n => "(uintptr_t)" ++ print_name n) |
| 146 |
all_args)) |
| 147 |
++ ")")%string. |
| 148 |
|
| 149 |
Local Instance Decidable_Bitwidth width |
| 150 |
: Decidable.Decidable (Bitwidth.Bitwidth width). |
| 151 |
Proof. |
| 152 |
eapply Decidable.Decidable_iff_to_impl. |
| 153 |
{ split. { intros H. split. exact H. } { intros [H]. exact H. } } |
| 154 |
exact _. |
| 155 |
Qed. |
| 156 |
|
| 157 |
(** When writing wrapper functions, we first check to see if any of the arguments fit in uint8_t; if not then we use the given bounds relaxation function *) |
| 158 |
Notation wrapper_relax_zrange relax_zrange |
| 159 |
:= (fun r => Option.value (BoundsPipeline.relax_zrange_gen false (* only signed=false *) [8]%Z r) (relax_zrange r)). |
| 160 |
|
| 161 |
(* TODO: for now, name_list is just ignored -- could probably make it not ignored *) |
| 162 |
Definition Bedrock2_ToFunctionLines |
| 163 |
{opts : AbstractInterpretation.Options} |
| 164 |
{relax_zrange : relax_zrange_opt} |
| 165 |
{language_naming_conventions : language_naming_conventions_opt} |
| 166 |
{documentation_options : documentation_options_opt} |
| 167 |
{output_options : output_options_opt} |
| 168 |
(width : Z) |
| 169 |
(do_bounds_check : bool) (internal_static : bool) (static : bool) (all_static : bool) (inline : bool) (prefix : string) (name : string) |
| 170 |
{t} |
| 171 |
(e : @API.Expr t) |
| 172 |
(comment : type.for_each_lhs_of_arrow ToString.OfPHOAS.var_data t -> |
| 173 |
ToString.OfPHOAS.var_data (type.base (type.final_codomain t)) -> list string) |
| 174 |
(name_list : option (list string)) |
| 175 |
(inbounds : type.for_each_lhs_of_arrow ZRange.type.option.interp t) |
| 176 |
(outbounds : ZRange.type.base.option.interp (type.final_codomain t)) |
| 177 |
(intypedefs : type.for_each_lhs_of_arrow var_typedef_data t) |
| 178 |
(outtypedefs : base_var_typedef_data (type.final_codomain t)) |
| 179 |
: (list string * ToString.ident_infos) + string |
| 180 |
:= |
| 181 |
match Decidable.dec (0 < width)%Z, Decidable.dec (Bitwidth.Bitwidth width) with |
| 182 |
| left width_pos, left BW => |
| 183 |
let p : Types.parameters |
| 184 |
(BW:=BW) |
| 185 |
(word:=Naive.word width) |
| 186 |
(locals:=SortedListString.map _) |
| 187 |
(mem:=SortedListWord.map(word_ok:=Naive.ok width width_pos) _ _) |
| 188 |
(ext_spec:=fun _ _ _ _ _ => False) |
| 189 |
(varname_gen := default_varname_gen) |
| 190 |
(error := expr.var Defaults.ERROR) |
| 191 |
:= tt in |
| 192 |
let innames := make_innames (parameters_sentinel:=p)(inname_gen:=default_inname_gen) t in |
| 193 |
let outnames := make_outnames (outname_gen:=default_outname_gen) |
| 194 |
(type.final_codomain t) in |
| 195 |
match list_lengths_from_argbounds inbounds with |
| 196 |
| Some inlengths => |
| 197 |
match make_access_sizes_args inbounds, |
| 198 |
make_base_access_sizes outbounds with |
| 199 |
| Some insizes, Some outsizes => |
| 200 |
let out := translate_func |
| 201 |
e innames inlengths insizes outnames outsizes in |
| 202 |
let f : string*func := (("internal_" ++ name)%string, fst out) in |
| 203 |
let outlengths := snd out in |
| 204 |
if error_free_cmd (snd (snd f)) |
| 205 |
then |
| 206 |
match make_header innames inlengths inbounds |
| 207 |
outnames outlengths outbounds with |
| 208 |
| Some header => |
| 209 |
match (let relax_zrange : relax_zrange_opt := wrapper_relax_zrange relax_zrange in |
| 210 |
IR.OfPHOAS.var_data_of_PHOAS_bounds e name_list inbounds outbounds intypedefs outtypedefs) with |
| 211 |
| inl (indata, outdata) |
| 212 |
=> inl ((header ++ ["static"] ++ bedrock_func_to_lines f) |
| 213 |
++ ["/* NOTE: The following wrapper function is not covered by Coq proofs */" |
| 214 |
; (((if inline then "__inline__ " else "") ++ (if static then "static " else "") ++ "void " ++ name ++ "(") |
| 215 |
++ (String.concat ", " (C.to_retarg_list prefix outdata ++ C.to_arg_list_for_each_lhs_of_arrow prefix indata)) |
| 216 |
++ ") {") |
| 217 |
; " " ++ (wrap_call indata outdata f insizes outsizes) ++ ";" |
| 218 |
; "}" |
| 219 |
; "" |
| 220 |
]%string%list, |
| 221 |
ToString.ident_info_empty) |
| 222 |
| inr errs => inr (String.concat String.NewLine errs) |
| 223 |
end |
| 224 |
| None => |
| 225 |
inr |
| 226 |
(String.concat |
| 227 |
String.NewLine |
| 228 |
("Failed to generate header for function:" |
| 229 |
:: bedrock_func_to_lines f)) |
| 230 |
end |
| 231 |
else |
| 232 |
let header := |
| 233 |
match make_header innames inlengths inbounds |
| 234 |
outnames outlengths outbounds with |
| 235 |
| Some x => x | None => [] end in |
| 236 |
inr |
| 237 |
(String.concat |
| 238 |
String.NewLine |
| 239 |
(["ERROR-CONTAINING OUTPUT:"] |
| 240 |
++ header |
| 241 |
++ bedrock_func_to_lines f |
| 242 |
++ ["Error occured during translation to bedrock2. This is likely because a part of the input expression either had unsupported integer types (bedrock2 requires that all integers have the same size) or contained an unsupported operation."])) |
| 243 |
| None, _ => inr ("Error determining argument sizes for input bounds. Please check that the bounds fit within the machine word size.") |
| 244 |
| _, None => inr ("Error determining argument sizes for output bounds. Please check that the bounds fit within the machine word size.") |
| 245 |
end |
| 246 |
| None => |
| 247 |
inr ("Error determining argument lengths from input bounds") |
| 248 |
end |
| 249 |
| _,_ => inr ("Only 32-bit and 64-bit targets are supported") |
| 250 |
end. |
| 251 |
|
| 252 |
Definition OutputBedrock2API : ToString.OutputLanguageAPI := |
| 253 |
{| |
| 254 |
ToString.comment_block s |
| 255 |
:= List.map (fun line => "/* " ++ line ++ " */")%string s; |
| 256 |
|
| 257 |
ToString.comment_file_header_block s |
| 258 |
:= List.map (fun line => "/* " ++ line ++ " */")%string s; |
| 259 |
|
| 260 |
ToString.ToFunctionLines := @Bedrock2_ToFunctionLines; |
| 261 |
|
| 262 |
ToString.header := fun _ _ _ _ _ _ _ _ _ _ _ => [""; ToCString.prelude]; |
| 263 |
|
| 264 |
ToString.footer := fun _ _ _ _ _ _ _ _ _ => []; |
| 265 |
|
| 266 |
(** No special handling for any functions *) |
| 267 |
ToString.strip_special_infos machine_wordsize infos := infos; |
| 268 |
|
| 269 |
|}. |