| 1 |
From Coq Require Import ZArith MSetPositive FMapPositive |
| 2 |
String Ascii Bool List HexString. |
| 3 |
From Crypto.Util Require Import |
| 4 |
ListUtil |
| 5 |
Strings.String Strings.Decimal Strings.Show |
| 6 |
ZRange.Operations ZRange.Show |
| 7 |
OptionList Bool.Equality. |
| 8 |
Require Import Crypto.Util.Option. |
| 9 |
(* Work around COQBUG(https://github.com/coq/coq/issues/12251) *) |
| 10 |
Require Import Crypto.Util.ZRange. |
| 11 |
|
| 12 |
From Crypto Require Import IR Stringification.Language AbstractInterpretation.ZRange. |
| 13 |
|
| 14 |
Import ListNotations. |
| 15 |
|
| 16 |
Local Open Scope list_scope. |
| 17 |
Local Open Scope string_scope. |
| 18 |
Local Open Scope zrange_scope. |
| 19 |
Local Open Scope Z_scope. |
| 20 |
|
| 21 |
Import IR.Compilers.ToString. |
| 22 |
Import Stringification.Language.Compilers. |
| 23 |
Import Stringification.Language.Compilers.Options. |
| 24 |
Import Stringification.Language.Compilers.ToString. |
| 25 |
Import Stringification.Language.Compilers.ToString.int.Notations. |
| 26 |
|
| 27 |
Module Go. |
| 28 |
|
| 29 |
Definition comment_block (lines : list string) : list string |
| 30 |
:= match lines with |
| 31 |
| [] => [] |
| 32 |
| [""] => [""] |
| 33 |
| [line] => ["// " ++ line]%string |
| 34 |
| lines => List.map (fun s => if (String.length s =? 0)%nat then "//" else "// " ++ s)%string lines |
| 35 |
end%list%string. |
| 36 |
|
| 37 |
Definition comment_block_extra_newlines (lines : list string) : list string |
| 38 |
:= match lines with |
| 39 |
| [] => [] |
| 40 |
| [""] => [""] |
| 41 |
| [line] => ["// " ++ line]%string |
| 42 |
| lines => List.tl (List.flat_map (fun s => ["//"; |
| 43 |
if (String.length s =? 0)%nat |
| 44 |
then "//" |
| 45 |
else "// " ++ s])%string lines) |
| 46 |
end%list%string. |
| 47 |
|
| 48 |
(* Supported integer bitwidths *) |
| 49 |
Definition stdint_bitwidths : list Z := [8; 16; 32; 64]. |
| 50 |
(* supported bitwidth for things like bits.Mul64 *) |
| 51 |
Definition bits_std_bitwidths : list Z := [32; 64]. |
| 52 |
Definition is_bits_standard_bitwidth (bw : Z) := existsb (Z.eqb bw) bits_std_bitwidths. |
| 53 |
Definition is_standard_bitwidth (bw : Z) := existsb (Z.eqb bw) stdint_bitwidths. |
| 54 |
Definition is_special_bitwidth (bw : Z) := negb (is_standard_bitwidth bw). |
| 55 |
Definition is_nonbits_special_bitwidth (bw : Z) := negb (is_bits_standard_bitwidth bw). |
| 56 |
|
| 57 |
Definition int_type_to_string_opt_typedef {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (private : bool) (all_private : bool) (prefix : string) (t : ToString.int.type) (typedef : option string) : string := |
| 58 |
int.to_string_gen_opt_typedef stdint_bitwidths "uint" "int" "" "" private all_private prefix t typedef. |
| 59 |
Definition int_type_to_string {language_naming_conventions : language_naming_conventions_opt} (private : bool) (prefix : string) (t : ToString.int.type) : string := |
| 60 |
int_type_to_string_opt_typedef (skip_typedefs:=true) private private prefix t None. |
| 61 |
|
| 62 |
Definition make_typedef |
| 63 |
{language_naming_conventions : language_naming_conventions_opt} |
| 64 |
{documentation_options : documentation_options_opt} |
| 65 |
(prefix : string) (internal_private : bool) (private : bool) |
| 66 |
(typedef : typedef_info) |
| 67 |
: list string |
| 68 |
:= let '(name, (ty, array_len), description) := name_and_type_and_describe_typedef prefix private typedef in |
| 69 |
((comment_block_extra_newlines description) |
| 70 |
++ ["type " |
| 71 |
++ name ++ " " ++ |
| 72 |
match array_len with |
| 73 |
| None (* just an integer *) => "" |
| 74 |
| Some None (* unknown array length *) => "[]" |
| 75 |
| Some (Some len) => "[" ++ Decimal.Z.to_string (Z.of_nat len) ++ "]" |
| 76 |
end |
| 77 |
++ match ty with |
| 78 |
| Some ty => int_type_to_string internal_private prefix ty |
| 79 |
| None => "ℤ" (* blackboard bold Z for unbounded integers (which don't actually exist, and thus will error) *) |
| 80 |
end]%string)%list. |
| 81 |
|
| 82 |
(* Header imports and type defs *) |
| 83 |
Definition header |
| 84 |
{language_naming_conventions : language_naming_conventions_opt} |
| 85 |
{documentation_options : documentation_options_opt} |
| 86 |
{package_namev : package_name_opt} |
| 87 |
{class_namev : class_name_opt} |
| 88 |
{output_options : output_options_opt} |
| 89 |
(machine_wordsize : Z) (internal_private : bool) (private : bool) (prefix : string) (infos : ToString.ident_infos) |
| 90 |
(typedef_map : list typedef_info) |
| 91 |
: list string |
| 92 |
:= let bitwidths_used := ToString.bitwidths_used infos in |
| 93 |
let needs_bits_import (* we only need the bits import if we use addcarryx/subborrowx/mulx with a standard bitwidth *) |
| 94 |
:= PositiveSet.exists_ |
| 95 |
(fun bw => is_bits_standard_bitwidth (Z.pos bw)) |
| 96 |
(PositiveSet.union (ToString.addcarryx_lg_splits infos) |
| 97 |
(ToString.mulx_lg_splits infos)) in |
| 98 |
let type_prefix := "type "%string in |
| 99 |
let special_addcarryx_lg_splits |
| 100 |
:= List.filter |
| 101 |
(fun bw => negb (List.existsb (Z.eqb bw) relax_adc_sbb_return_carry_to_bitwidth)) |
| 102 |
(List.filter |
| 103 |
is_bits_standard_bitwidth |
| 104 |
(List.map |
| 105 |
Z.pos |
| 106 |
(PositiveSet.elements (ToString.addcarryx_lg_splits infos)))) in |
| 107 |
let carry_type := (if IntSet.mem _Bool bitwidths_used || IntSet.mem (ToString.int.signed_counterpart_of _Bool) bitwidths_used |
| 108 |
then int_type_to_string internal_private prefix _Bool |
| 109 |
else if IntSet.mem uint8 bitwidths_used || IntSet.mem int8 bitwidths_used |
| 110 |
then int_type_to_string internal_private prefix uint8 |
| 111 |
else int_type_to_string internal_private prefix (int.of_bitwidth false(*unsigned*) machine_wordsize)) in |
| 112 |
strip_trailing_newlines |
| 113 |
(((if newline_before_package_declaration then [""] else []) |
| 114 |
++ ["package " ++ package_name prefix; |
| 115 |
""]%string) |
| 116 |
++ (if needs_bits_import then ["import ""math/bits"""; ""]%string else []) |
| 117 |
++ (let typedefs |
| 118 |
:= let carry_bitwidth := 64 (* c.f. https://github.com/mit-plv/fiat-crypto/pull/1006#issuecomment-892625927 *) in |
| 119 |
let carry_typedef_comment := (" // We use uint" ++ Decimal.Z.to_string carry_bitwidth ++ " instead of a more narrow type for performance reasons; see https://github.com/mit-plv/fiat-crypto/pull/1006#issuecomment-892625927")%string in |
| 120 |
List.flat_map |
| 121 |
(fun bw |
| 122 |
=> (if IntSet.mem (int.of_bitwidth false bw) bitwidths_used || IntSet.mem (int.of_bitwidth true bw) bitwidths_used |
| 123 |
then [type_prefix ++ int_type_to_string internal_private prefix (int.of_bitwidth false bw) ++ " uint" ++ Decimal.Z.to_string carry_bitwidth ++ carry_typedef_comment; |
| 124 |
type_prefix ++ int_type_to_string internal_private prefix (int.of_bitwidth true bw) ++ " int" ++ Decimal.Z.to_string carry_bitwidth ++ carry_typedef_comment]%string |
| 125 |
else [])) |
| 126 |
[1; 2] in |
| 127 |
let typedefs := |
| 128 |
typedefs |
| 129 |
++ if skip_typedefs |
| 130 |
then [] |
| 131 |
else List.flat_map |
| 132 |
(fun td_name => |
| 133 |
match List.find (fun '(name, _, _, _) => (td_name =? name)%string) typedef_map with |
| 134 |
| Some td_info => [""] ++ make_typedef prefix internal_private private td_info |
| 135 |
| None => ["var _ = error_Go_Could_not_find_typedef_info_for_" ++ td_name]%string |
| 136 |
end%list) |
| 137 |
(typedefs_used infos) in |
| 138 |
match typedefs with |
| 139 |
| [] => [] |
| 140 |
| _::_ => typedefs ++ [""] |
| 141 |
end) |
| 142 |
++ (if IntSet.mem uint128 bitwidths_used || IntSet.mem int128 bitwidths_used |
| 143 |
then ["var _ = error_Go_output_does_not_support_128_bit_integers___instead_use_rewriting_rules_for_removing_128_bit_integers"]%string |
| 144 |
else []) |
| 145 |
++ (List.tl |
| 146 |
(List.flat_map |
| 147 |
(fun bw |
| 148 |
=> let s_bw := Decimal.Z.to_string bw in |
| 149 |
List.flat_map |
| 150 |
(fun '(newname, bitsname) |
| 151 |
=> [""%string] |
| 152 |
++ (comment_block |
| 153 |
[text_before_function_name ++ newname ++ " is a thin wrapper around " ++ bitsname ++ " that uses " ++ carry_type ++ " rather than uint" ++ s_bw]%string) |
| 154 |
++ ["func " ++ newname ++ "(x uint" ++ s_bw ++ ", y uint" ++ s_bw ++ ", carry " ++ carry_type ++ ") (uint" ++ s_bw ++ ", " ++ carry_type ++ ") {" |
| 155 |
; String.Tab ++ "sum, carryOut := " ++ bitsname ++ "(x, y, uint" ++ s_bw ++ "(carry))" |
| 156 |
; String.Tab ++ "return sum, " ++ carry_type ++ "(carryOut)" |
| 157 |
; "}"]%string) |
| 158 |
[(ToString.format_special_function_name internal_private prefix "addcarryx" false(*unsigned*) bw, "bits.Add" ++ s_bw) |
| 159 |
; (ToString.format_special_function_name internal_private prefix "subborrowx" false(*unsigned*) bw, "bits.Sub" ++ s_bw)]%string) |
| 160 |
special_addcarryx_lg_splits)))%list. |
| 161 |
|
| 162 |
(* Instead of "macros for minimum-width integer constants" we tried to |
| 163 |
use numeric casts in Go. It turns out that it wasn't needed and Go |
| 164 |
will figure out the types of the litterals, so disabling this for |
| 165 |
now *) |
| 166 |
Definition cast_literal (prefix : string) (t : ToString.int.type) : option string := |
| 167 |
if Z.ltb (ToString.int.bitwidth_of t) 8 |
| 168 |
then None |
| 169 |
else None. |
| 170 |
|
| 171 |
|
| 172 |
(* (Comment modified from) Zoe: In fiat-crypto C functions are void |
| 173 |
and as such, they receive pointers to the result as |
| 174 |
argumnets. The C code before calling a function, declares |
| 175 |
uninitializedinteger pointers and passes them as arguments. In |
| 176 |
Go to do that, we declare an initialized (to 0) mutable, and pass |
| 177 |
a mutable reference to the callee. |
| 178 |
|
| 179 |
In the longer term, we should examine whether we should use |
| 180 |
non-void functions in Go and just return the result *) |
| 181 |
|
| 182 |
Definition primitive_type_to_string_opt_typedef |
| 183 |
{language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} |
| 184 |
(private : bool) (all_private : bool) |
| 185 |
(prefix : string) (t : IR.type.primitive) |
| 186 |
(r : option ToString.int.type) (typedef : option string) : string := |
| 187 |
match t with |
| 188 |
| IR.type.Zptr => "*" |
| 189 |
| IR.type.Z => "" |
| 190 |
end ++ match r with |
| 191 |
| Some int_t => int_type_to_string_opt_typedef private all_private prefix int_t typedef |
| 192 |
| None => "ℤ" (* blackboard bold Z for unbounded integers (which don't actually exist, and thus will error) *) |
| 193 |
end. |
| 194 |
Definition primitive_type_to_string |
| 195 |
{language_naming_conventions : language_naming_conventions_opt} (private : bool) |
| 196 |
(prefix : string) (t : IR.type.primitive) |
| 197 |
(r : option ToString.int.type) : string := |
| 198 |
primitive_type_to_string_opt_typedef (skip_typedefs:=true) private private prefix t r None. |
| 199 |
Definition primitive_array_type_to_string |
| 200 |
{language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} |
| 201 |
(internal_private : bool) (all_private : bool) |
| 202 |
(prefix : string) (t : IR.type.primitive) |
| 203 |
(r : option ToString.int.type) (len : nat) (typedef : option string) : string := |
| 204 |
match (if skip_typedefs then None else typedef) with |
| 205 |
| Some typedef => format_typedef_name prefix all_private typedef |
| 206 |
| None => "[" ++ Decimal.Z.to_string (Z.of_nat len) ++ "]" ++ |
| 207 |
primitive_type_to_string internal_private prefix t r |
| 208 |
end. |
| 209 |
|
| 210 |
(* Integer literal to string *) |
| 211 |
Definition int_literal_to_string (prefix : string) (t : IR.type.primitive) (v : BinInt.Z) : string := |
| 212 |
match t, cast_literal prefix (ToString.int.of_zrange_relaxed r[v ~> v]) with |
| 213 |
| IR.type.Z, Some cast => "(" ++ HexString.of_Z v ++ cast ++ ")" |
| 214 |
| IR.type.Z, None => (* just print hex value, no cast *) HexString.of_Z v |
| 215 |
| IR.type.Zptr, _ => "var _error = literal_address_" ++ HexString.of_Z v |
| 216 |
end. |
| 217 |
|
| 218 |
Import IR.Notations. |
| 219 |
|
| 220 |
Fixpoint arith_to_string |
| 221 |
{language_naming_conventions : language_naming_conventions_opt} |
| 222 |
{relax_adc_sbb_return_carry_to_bitwidth : relax_adc_sbb_return_carry_to_bitwidth_opt} |
| 223 |
(internal_private : bool) (prefix : string) |
| 224 |
{t} (e : IR.arith_expr t) |
| 225 |
: string |
| 226 |
:= let special_name_ty name ty := ToString.format_special_function_name_ty internal_private prefix name ty in |
| 227 |
let special_name name bw := ToString.format_special_function_name internal_private prefix name false(*unsigned*) bw in |
| 228 |
match e with |
| 229 |
(* integer literals *) |
| 230 |
| (IR.literal v @@@ _) => int_literal_to_string prefix IR.type.Z v |
| 231 |
(* array dereference *) |
| 232 |
| (IR.List_nth n @@@ IR.Var _ v) => v ++ "[" ++ Decimal.Z.to_string (Z.of_nat n) ++ "]" |
| 233 |
(* (de)referencing *) |
| 234 |
| (IR.Addr @@@ IR.Var _ v) => "&" ++ v |
| 235 |
| (IR.Dereference @@@ e) => "( *" ++ arith_to_string internal_private prefix e ++ " )" |
| 236 |
(* bitwise operations *) |
| 237 |
| (IR.Z_shiftr offset @@@ e) => |
| 238 |
"(" ++ arith_to_string internal_private prefix e ++ " >> " ++ Decimal.Z.to_string offset ++ ")" |
| 239 |
| (IR.Z_shiftl offset @@@ e) => |
| 240 |
"(" ++ arith_to_string internal_private prefix e ++ " << " ++ Decimal.Z.to_string offset ++ ")" |
| 241 |
| (IR.Z_land @@@ (e1, e2)) => |
| 242 |
"(" ++ arith_to_string internal_private prefix e1 ++ " & " ++ arith_to_string internal_private prefix e2 ++ ")" |
| 243 |
| (IR.Z_lor @@@ (e1, e2)) => |
| 244 |
"(" ++ arith_to_string internal_private prefix e1 ++ " | " ++ arith_to_string internal_private prefix e2 ++ ")" |
| 245 |
| (IR.Z_lxor @@@ (e1, e2)) => |
| 246 |
"(" ++ arith_to_string internal_private prefix e1 ++ " ^ " ++ arith_to_string internal_private prefix e2 ++ ")" |
| 247 |
| (IR.Z_lnot _ @@@ e) => "(^" ++ arith_to_string internal_private prefix e ++ ")" |
| 248 |
(* arithmetic operations *) |
| 249 |
| (IR.Z_add @@@ (x1, x2)) => |
| 250 |
"(" ++ arith_to_string internal_private prefix x1 ++ " + " ++ arith_to_string internal_private prefix x2 ++ ")" |
| 251 |
| (IR.Z_mul @@@ (x1, x2)) => |
| 252 |
"(" ++ arith_to_string internal_private prefix x1 ++ " * " ++ arith_to_string internal_private prefix x2 ++ ")" |
| 253 |
| (IR.Z_sub @@@ (x1, x2)) => |
| 254 |
"(" ++ arith_to_string internal_private prefix x1 ++ " - " ++ arith_to_string internal_private prefix x2 ++ ")" |
| 255 |
| (IR.Z_bneg @@@ e) => "(!/* TODO: FIX ME */ " ++ arith_to_string internal_private prefix e ++ ")" |
| 256 |
| (IR.Z_mul_split lg2s @@@ args) => |
| 257 |
match is_bits_standard_bitwidth lg2s, args with |
| 258 |
| true, ((IR.Addr @@@ lo, IR.Addr @@@ hi), args) |
| 259 |
=> (arith_to_string internal_private prefix hi ++ ", " ++ arith_to_string internal_private prefix lo) |
| 260 |
++ " = bits.Mul" |
| 261 |
++ Decimal.Z.to_string lg2s ++ "(" ++ arith_to_string internal_private prefix args ++ ")" |
| 262 |
| _, _ |
| 263 |
=> special_name "mulx" lg2s ++ "(" ++ arith_to_string internal_private prefix args ++ ")" |
| 264 |
end |
| 265 |
| (IR.Z_add_with_get_carry lg2s @@@ args) => |
| 266 |
let (func_name, wrap_carry) |
| 267 |
:= (if List.existsb (Z.eqb lg2s) relax_adc_sbb_return_carry_to_bitwidth |
| 268 |
then ("bits.Add" ++ Decimal.Z.to_string lg2s, fun c => "uint" ++ Decimal.Z.to_string lg2s ++ "(" ++ c ++ ")") |
| 269 |
else (special_name "addcarryx" lg2s, fun c => c))%core in |
| 270 |
match is_bits_standard_bitwidth lg2s, args with |
| 271 |
| true, ((IR.Addr @@@ v, IR.Addr @@@ c), (cin, x, y)) |
| 272 |
=> (arith_to_string internal_private prefix v ++ ", " ++ arith_to_string internal_private prefix c) |
| 273 |
++ " = " ++ func_name |
| 274 |
++ "(" ++ arith_to_string internal_private prefix x ++ ", " ++ arith_to_string internal_private prefix y ++ ", " ++ wrap_carry (arith_to_string internal_private prefix cin) ++ ")" |
| 275 |
| _, _ |
| 276 |
=> special_name "addcarryx" lg2s ++ "(" ++ arith_to_string internal_private prefix args ++ ")" |
| 277 |
end |
| 278 |
| (IR.Z_sub_with_get_borrow lg2s @@@ args) => |
| 279 |
let (func_name, wrap_carry) |
| 280 |
:= (if List.existsb (Z.eqb lg2s) relax_adc_sbb_return_carry_to_bitwidth |
| 281 |
then ("bits.Sub" ++ Decimal.Z.to_string lg2s, fun c => "uint" ++ Decimal.Z.to_string lg2s ++ "(" ++ c ++ ")") |
| 282 |
else (special_name "subborrowx" lg2s, fun c => c))%core in |
| 283 |
match is_bits_standard_bitwidth lg2s, args with |
| 284 |
| true, ((IR.Addr @@@ v, IR.Addr @@@ b), (bin, x, y)) |
| 285 |
=> (arith_to_string internal_private prefix v ++ ", " ++ arith_to_string internal_private prefix b) |
| 286 |
++ " = " ++ func_name |
| 287 |
++ "(" ++ arith_to_string internal_private prefix x ++ ", " ++ arith_to_string internal_private prefix y ++ ", " ++ wrap_carry (arith_to_string internal_private prefix bin) ++ ")" |
| 288 |
| _, _ |
| 289 |
=> special_name "subborrowx" lg2s ++ "(" ++ arith_to_string internal_private prefix args ++ ")" |
| 290 |
end |
| 291 |
| (IR.Z_value_barrier ty @@@ args) => |
| 292 |
special_name_ty "value_barrier" ty ++ "(" ++ arith_to_string internal_private prefix args ++ ")" |
| 293 |
| (IR.Z_zselect ty @@@ args) => |
| 294 |
special_name_ty "cmovznz" ty ++ "(" ++ arith_to_string internal_private prefix args ++ ")" |
| 295 |
| (IR.Z_static_cast int_t @@@ e) => |
| 296 |
primitive_type_to_string internal_private prefix IR.type.Z (Some int_t) ++ "(" ++ arith_to_string internal_private prefix e ++ ")" |
| 297 |
| IR.Var _ v => v |
| 298 |
| IR.Pair A B a b => arith_to_string internal_private prefix a ++ ", " ++ arith_to_string internal_private prefix b |
| 299 |
| (IR.Z_add_modulo @@@ (x1, x2, x3)) => "var _error = error_addmodulo" |
| 300 |
| (IR.List_nth _ @@@ _) |
| 301 |
| (IR.Addr @@@ _) |
| 302 |
| (IR.Z_add @@@ _) |
| 303 |
| (IR.Z_mul @@@ _) |
| 304 |
| (IR.Z_sub @@@ _) |
| 305 |
| (IR.Z_land @@@ _) |
| 306 |
| (IR.Z_lor @@@ _) |
| 307 |
| (IR.Z_lxor @@@ _) |
| 308 |
| (IR.Z_add_modulo @@@ _) => "var _error = error_bad_arg" |
| 309 |
| IR.TT => "var _error = error_tt" |
| 310 |
end%string%Cexpr. |
| 311 |
|
| 312 |
(** In Go, we munge return arguments of some functions above, so |
| 313 |
there is the possibility for them to become unused *) |
| 314 |
Local Instance : IR.OfPHOAS.consider_retargs_live_opt |
| 315 |
:= fun _ _ idc |
| 316 |
=> match idc with |
| 317 |
| IR.Z_mul_split lg2s |
| 318 |
| IR.Z_add_with_get_carry lg2s |
| 319 |
| IR.Z_sub_with_get_borrow lg2s |
| 320 |
(* these are munged; having a variable be used as a return |
| 321 |
from these functions doesn't imply that it is live *) |
| 322 |
=> is_nonbits_special_bitwidth lg2s |
| 323 |
| IR.literal _ |
| 324 |
| IR.List_nth _ |
| 325 |
| IR.Addr |
| 326 |
| IR.Dereference |
| 327 |
| IR.iunop _ |
| 328 |
| IR.ibinop _ |
| 329 |
| IR.Z_zselect _ |
| 330 |
| IR.Z_add_modulo |
| 331 |
| IR.Z_static_cast _ |
| 332 |
=> true (* these are not munged *) |
| 333 |
end. |
| 334 |
(** In Go, assignment to an unused variable can be replaced with assignment to _ *) |
| 335 |
Local Instance : IR.OfPHOAS.rename_dead_opt := fun _ => "_"%string. |
| 336 |
(** No need to lift declarations to the top *) |
| 337 |
Local Instance : IR.OfPHOAS.lift_declarations_opt := false. |
| 338 |
|
| 339 |
Definition stmt_to_string |
| 340 |
{language_naming_conventions : language_naming_conventions_opt} |
| 341 |
{relax_adc_sbb_return_carry_to_bitwidth : relax_adc_sbb_return_carry_to_bitwidth_opt} |
| 342 |
(internal_private : bool) (prefix : string) |
| 343 |
(e : IR.stmt) |
| 344 |
: string := |
| 345 |
match e with |
| 346 |
| IR.Call val => arith_to_string internal_private prefix val |
| 347 |
| IR.Assign true t sz name val => |
| 348 |
(* local non-mutable declaration with initialization *) |
| 349 |
name ++ " := " ++ arith_to_string internal_private prefix val |
| 350 |
| IR.Assign false _ sz name val => |
| 351 |
(* This corresponds to assignment to a non-pointer variable and should never ever |
| 352 |
happen in our generated code. Fiat-crypto handles it but I |
| 353 |
haven't found and instance of this to their generated code *) |
| 354 |
(* code : name ++ " = " ++ arith_to_string internal_private prefix val ++ ";" *) |
| 355 |
"var _error = error_trying_to_assign_value_to_non_mutable_variable" |
| 356 |
| IR.AssignZPtr name sz val => |
| 357 |
"*" ++ name ++ " = " ++ arith_to_string internal_private prefix val |
| 358 |
| IR.DeclareVar t sz name => |
| 359 |
(* Local uninitialized declarations become mut declarations, and |
| 360 |
are initialized to 0. *) |
| 361 |
(* The assumptions here, based on fiat-crypto code generation |
| 362 |
patterns, are that 1.) this variable will be an argument to a |
| 363 |
call that will store its result in this variable. 2.) this will |
| 364 |
have a non-pointer an integer type *) |
| 365 |
"var " ++ name ++ " " ++ primitive_type_to_string internal_private prefix t sz |
| 366 |
| IR.Comment lines _ => |
| 367 |
String.concat String.NewLine (comment_block (ToString.preprocess_comment_block lines)) |
| 368 |
| IR.AssignNth name n val => |
| 369 |
name ++ "[" ++ Decimal.Z.to_string (Z.of_nat n) ++ "] = " ++ arith_to_string internal_private prefix val |
| 370 |
end. |
| 371 |
|
| 372 |
Definition to_strings |
| 373 |
{language_naming_conventions : language_naming_conventions_opt} |
| 374 |
{relax_adc_sbb_return_carry_to_bitwidth : relax_adc_sbb_return_carry_to_bitwidth_opt} |
| 375 |
(internal_private : bool) (prefix : string) |
| 376 |
(e : IR.expr) |
| 377 |
: list string := |
| 378 |
List.map (stmt_to_string internal_private prefix) e. |
| 379 |
|
| 380 |
Import Rewriter.Language.Language.Compilers Crypto.Language.API.Compilers IR.OfPHOAS. |
| 381 |
Local Notation tZ := (base.type.type_base base.type.Z). |
| 382 |
|
| 383 |
Inductive Mode := In | Out. |
| 384 |
|
| 385 |
|
| 386 |
(* This would have been nice, but coercions don't work *) |
| 387 |
(* Module Base := Rewriter.Language.Language.Compilers.base. *) |
| 388 |
|
| 389 |
Fixpoint to_base_arg_list {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (internal_private : bool) (all_private : bool) (prefix : string) (mode : Mode) {t} : ToString.OfPHOAS.base_var_data t -> list string := |
| 390 |
match t return base_var_data t -> _ with |
| 391 |
| tZ => |
| 392 |
let typ := match mode with In => IR.type.Z | Out => IR.type.Zptr end in |
| 393 |
fun '(n, is_ptr, r, typedef) => [n ++ " " ++ primitive_type_to_string_opt_typedef internal_private all_private prefix typ r typedef] |
| 394 |
| base.type.prod A B => |
| 395 |
fun '(va, vb) => (to_base_arg_list internal_private all_private prefix mode va ++ to_base_arg_list internal_private all_private prefix mode vb)%list |
| 396 |
| base.type.list tZ => |
| 397 |
fun '(n, r, len, typedef) => |
| 398 |
[ n ++ " *" ++ primitive_array_type_to_string internal_private all_private prefix IR.type.Z r len typedef] |
| 399 |
| base.type.list _ => fun _ => ["var _error = error_complex_list"] |
| 400 |
| base.type.option _ => fun _ => ["var _error = error_option"] |
| 401 |
| base.type.unit => fun _ => ["var _error = error_unit"] |
| 402 |
| base.type.type_base t => fun _ => ["var _error = error_" ++ show t]%string |
| 403 |
end%string. |
| 404 |
|
| 405 |
Definition to_arg_list {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (internal_private : bool) (all_private : bool) (prefix : string) (mode : Mode) {t} : var_data t -> list string := |
| 406 |
match t return var_data t -> _ with |
| 407 |
| type.base t => to_base_arg_list internal_private all_private prefix mode |
| 408 |
| type.arrow _ _ => fun _ => ["var _error = error_arrow"] |
| 409 |
end%string. |
| 410 |
|
| 411 |
Fixpoint to_arg_list_for_each_lhs_of_arrow {language_naming_conventions : language_naming_conventions_opt} {skip_typedefs : skip_typedefs_opt} (internal_private : bool) (all_private : bool) (prefix : string) {t} : type.for_each_lhs_of_arrow var_data t -> list string |
| 412 |
:= match t return type.for_each_lhs_of_arrow var_data t -> _ with |
| 413 |
| type.base t => fun _ => nil |
| 414 |
| type.arrow s d |
| 415 |
=> fun '(x, xs) |
| 416 |
=> to_arg_list internal_private all_private prefix In x ++ to_arg_list_for_each_lhs_of_arrow internal_private all_private prefix xs |
| 417 |
end%list. |
| 418 |
|
| 419 |
(** * Language-specific numeric conversions to be passed to the PHOAS -> IR translation *) |
| 420 |
|
| 421 |
Definition Go_bin_op_natural_output |
| 422 |
: IR.Z_binop -> ToString.int.type * ToString.int.type -> ToString.int.type |
| 423 |
:= fun idc '(t1, t2) |
| 424 |
=> ToString.int.union t1 t2. |
| 425 |
|
| 426 |
(* Does the binary operation commute with (-- mod 2^bw)? *) |
| 427 |
Definition bin_op_commutes_with_mod_pow2 (idc : IR.Z_binop) |
| 428 |
:= match idc with |
| 429 |
| IR.Z_land |
| 430 |
| IR.Z_lor |
| 431 |
| IR.Z_lxor |
| 432 |
| IR.Z_add |
| 433 |
| IR.Z_mul |
| 434 |
| IR.Z_sub |
| 435 |
=> true |
| 436 |
end. |
| 437 |
|
| 438 |
Definition Go_bin_op_casts |
| 439 |
: IR.Z_binop -> option ToString.int.type -> ToString.int.type * ToString.int.type -> option ToString.int.type * (option ToString.int.type * option ToString.int.type) |
| 440 |
:= fun idc desired_type '(t1, t2) |
| 441 |
=> match desired_type with |
| 442 |
| Some desired_type |
| 443 |
=> let ct := ToString.int.union t1 t2 in |
| 444 |
if bin_op_commutes_with_mod_pow2 idc |
| 445 |
then |
| 446 |
(* these operations commute with mod, so we just pre-cast them *) |
| 447 |
(None, (Some desired_type, Some desired_type)) |
| 448 |
else |
| 449 |
let desired_type' := Some (ToString.int.union ct desired_type) in |
| 450 |
(desired_type', |
| 451 |
(get_Zcast_up_if_needed desired_type' (Some t1), |
| 452 |
get_Zcast_up_if_needed desired_type' (Some t2))) |
| 453 |
| None => (None, (None, None)) |
| 454 |
end. |
| 455 |
|
| 456 |
Definition Go_un_op_casts |
| 457 |
: IR.Z_unop -> option ToString.int.type -> ToString.int.type -> option ToString.int.type * option ToString.int.type |
| 458 |
:= fun idc desired_type t |
| 459 |
=> match idc with |
| 460 |
| IR.Z_shiftr offset |
| 461 |
=> (** N.B. We must cast the expression up to a large |
| 462 |
enough type to fit 2^offset (importantly, not just |
| 463 |
2^offset-1), because C considers it to be undefined |
| 464 |
behavior to shift >= width of the type. We should |
| 465 |
probably figure out how to not generate these |
| 466 |
things in the first place... |
| 467 |
|
| 468 |
N.B. We must preserve signedness of the value being |
| 469 |
shifted, because shift does not commute with |
| 470 |
mod. *) |
| 471 |
let t' := ToString.int.union_zrange r[0~>2^offset]%zrange t in |
| 472 |
((** We cast the result down to the specified type, if needed *) |
| 473 |
get_Zcast_down_if_needed desired_type (Some t'), |
| 474 |
(** We cast the argument up to a large enough type *) |
| 475 |
get_Zcast_up_if_needed (Some t') (Some t)) |
| 476 |
| IR.Z_shiftl offset |
| 477 |
=> (** N.B. We must cast the expression up to a large |
| 478 |
enough type to fit 2^offset (importantly, not just |
| 479 |
2^offset-1), because C considers it to be undefined |
| 480 |
behavior to shift >= width of the type. We should |
| 481 |
probably figure out how to not generate these |
| 482 |
things in the first place... |
| 483 |
|
| 484 |
N.B. We make sure that we only left-shift unsigned |
| 485 |
values, since shifting into the sign bit is |
| 486 |
undefined behavior. *) |
| 487 |
let rpre_out := match desired_type with |
| 488 |
| Some rout => Some (ToString.int.union_zrange r[0~>2^offset] (ToString.int.unsigned_counterpart_of rout)) |
| 489 |
| None => Some (ToString.int.of_zrange_relaxed r[0~>2^offset]%zrange) |
| 490 |
end in |
| 491 |
((** We cast the result down to the specified type, if needed *) |
| 492 |
get_Zcast_down_if_needed desired_type rpre_out, |
| 493 |
(** We cast the argument up to a large enough type *) |
| 494 |
get_Zcast_up_if_needed rpre_out (Some t)) |
| 495 |
| IR.Z_lnot ty |
| 496 |
=> ((* if the result is too big, we cast it down; we |
| 497 |
don't need to upcast it because it'll get |
| 498 |
picked up by implicit casts if necessary *) |
| 499 |
get_Zcast_down_if_needed desired_type (Some ty), |
| 500 |
(** always cast to the width of the type, unless we are already exactly that type (which the machinery in IR handles *) |
| 501 |
Some ty) |
| 502 |
| IR.Z_value_barrier ty |
| 503 |
=> ((* if the result is too big, we cast it down; we |
| 504 |
don't need to upcast it because it'll get |
| 505 |
picked up by implicit casts if necessary *) |
| 506 |
get_Zcast_down_if_needed desired_type (Some ty), |
| 507 |
(** always cast to the width of the type, unless we are already exactly that type (which the machinery in IR handles *) |
| 508 |
Some ty) |
| 509 |
| IR.Z_bneg |
| 510 |
=> ((* bneg is !, i.e., takes the argument to 1 if its not zero, and to zero if it is zero; so we don't ever need to cast *) |
| 511 |
None, None) |
| 512 |
end. |
| 513 |
|
| 514 |
Local Instance GoLanguageCasts : LanguageCasts := |
| 515 |
{| bin_op_natural_output := Go_bin_op_natural_output |
| 516 |
; bin_op_casts := Go_bin_op_casts |
| 517 |
; un_op_casts := Go_un_op_casts |
| 518 |
; upcast_on_assignment := true |
| 519 |
; upcast_on_funcall := true |
| 520 |
; explicit_pointer_variables := false |
| 521 |
|}. |
| 522 |
|
| 523 |
Definition to_function_lines {language_naming_conventions : language_naming_conventions_opt} {output_options : output_options_opt} (internal_private : bool) (private : bool) (all_private : bool) (inline : bool) (prefix : string) (name : string) |
| 524 |
{t} |
| 525 |
(f : type.for_each_lhs_of_arrow var_data t * var_data (type.base (type.final_codomain t)) * IR.expr) |
| 526 |
: list string := |
| 527 |
let '(args, rets, body) := f in |
| 528 |
((("func " |
| 529 |
++ name |
| 530 |
++ "(" ++ String.concat ", " (to_arg_list internal_private all_private prefix Out rets ++ to_arg_list_for_each_lhs_of_arrow internal_private all_private prefix args) |
| 531 |
++ ") {")%string) |
| 532 |
:: (List.map (fun s => String.Tab ++ s)%string (to_strings internal_private prefix body)) |
| 533 |
++ ["}"%string])%list. |
| 534 |
|
| 535 |
Definition strip_special_infos (infos : ToString.ident_infos) : ToString.ident_infos |
| 536 |
:= ToString.ident_info_diff |
| 537 |
infos |
| 538 |
(List.fold_right |
| 539 |
ToString.ident_info_union |
| 540 |
ToString.ident_info_empty |
| 541 |
(List.flat_map |
| 542 |
(fun bw |
| 543 |
=> [IR.ident_infos.collect_infos_of_ident (IR.Z_mul_split bw) |
| 544 |
; IR.ident_infos.collect_infos_of_ident (IR.Z_add_with_get_carry bw) |
| 545 |
; IR.ident_infos.collect_infos_of_ident (IR.Z_sub_with_get_borrow bw)]) |
| 546 |
bits_std_bitwidths)). |
| 547 |
|
| 548 |
Definition ToFunctionLines |
| 549 |
{absint_opts : AbstractInterpretation.Options} |
| 550 |
{relax_zrange : relax_zrange_opt} |
| 551 |
{language_naming_conventions : language_naming_conventions_opt} |
| 552 |
{documentation_options : documentation_options_opt} |
| 553 |
{output_options : output_options_opt} |
| 554 |
(machine_wordsize : Z) |
| 555 |
(do_bounds_check : bool) (internal_private : bool) (private : bool) (all_private : bool) (inline : bool) (prefix : string) (name : string) |
| 556 |
{t} |
| 557 |
(e : API.Expr t) |
| 558 |
(comment : type.for_each_lhs_of_arrow var_data t -> var_data (type.base (type.final_codomain t)) -> list string) |
| 559 |
(name_list : option (list string)) |
| 560 |
(inbounds : type.for_each_lhs_of_arrow Compilers.ZRange.type.option.interp t) |
| 561 |
(outbounds : Compilers.ZRange.type.base.option.interp (type.final_codomain t)) |
| 562 |
(intypedefs : type.for_each_lhs_of_arrow var_typedef_data t) |
| 563 |
(outtypedefs : base_var_typedef_data (type.final_codomain t)) |
| 564 |
: (list string * ToString.ident_infos) + string := |
| 565 |
match ExprOfPHOAS do_bounds_check e name_list inbounds intypedefs outtypedefs with |
| 566 |
| inl (indata, outdata, f) => |
| 567 |
inl ((comment_block |
| 568 |
((comment indata outdata) |
| 569 |
++ match input_bounds_to_string indata inbounds with |
| 570 |
| nil => nil |
| 571 |
| ls => ["Input Bounds:"] ++ List.map (fun v => " " ++ v)%string ls |
| 572 |
end |
| 573 |
++ match bound_to_string outdata outbounds with |
| 574 |
| nil => nil |
| 575 |
| ls => ["Output Bounds:"] ++ List.map (fun v => " " ++ v)%string ls |
| 576 |
end)%list%string |
| 577 |
++ to_function_lines internal_private private all_private inline prefix name (indata, outdata, f))%list, |
| 578 |
IR.ident_infos.collect_all_infos f intypedefs outtypedefs) |
| 579 |
| inr nil => |
| 580 |
inr ("Unknown internal error in converting " ++ name ++ " to Go")%string |
| 581 |
| inr [err] => |
| 582 |
inr ("Error in converting " ++ name ++ " to Go:" ++ String.NewLine ++ err)%string |
| 583 |
| inr errs => |
| 584 |
inr ("Errors in converting " ++ name ++ " to Go:" ++ String.NewLine ++ String.concat String.NewLine errs)%string |
| 585 |
end. |
| 586 |
|
| 587 |
Definition OutputGoAPI : ToString.OutputLanguageAPI := |
| 588 |
{| ToString.comment_block := comment_block; |
| 589 |
ToString.comment_file_header_block := comment_block_extra_newlines; |
| 590 |
ToString.ToFunctionLines := @ToFunctionLines; |
| 591 |
ToString.header := @header; |
| 592 |
ToString.footer := fun _ _ _ _ _ _ _ _ _ => []; |
| 593 |
ToString.strip_special_infos machine_wordsize := strip_special_infos |}. |
| 594 |
|
| 595 |
End Go. |