| 1 |
From Coq Require Import String. |
| 2 |
From Coq Require Import List. |
| 3 |
Require Import bedrock2.Syntax. |
| 4 |
Require Import coqutil.Word.Interface. |
| 5 |
Require Import coqutil.Map.Interface. |
| 6 |
Require Import Crypto.Bedrock.Field.Common.Types. |
| 7 |
Require Import Crypto.Bedrock.Field.Translation.Cmd. |
| 8 |
Require Import Crypto.Bedrock.Field.Translation.Flatten. |
| 9 |
Require Import Crypto.Bedrock.Field.Translation.LoadStoreList. |
| 10 |
Require Import Crypto.Language.API. |
| 11 |
Import ListNotations. |
| 12 |
|
| 13 |
Import API.Compilers. |
| 14 |
Import Types.Notations. |
| 15 |
|
| 16 |
Section Func. |
| 17 |
Context |
| 18 |
{width BW word mem locals ext_spec varname_gen error} |
| 19 |
`{parameters_sentinel : @parameters width BW word mem locals ext_spec varname_gen error}. |
| 20 |
Existing Instance rep.Z. |
| 21 |
|
| 22 |
(* Feeds arguments to function one by one and then calls translate_cmd *) |
| 23 |
Fixpoint translate_func' {t} (e : @API.expr ltype t) (nextn : nat) |
| 24 |
: type.for_each_lhs_of_arrow ltype t -> (* args *) |
| 25 |
nat * base_ltype (type.final_codomain t) * cmd.cmd := |
| 26 |
match e with |
| 27 |
| expr.Abs (type.base s) d f => |
| 28 |
(* if we have an abs, peel off one argument and recurse *) |
| 29 |
fun (args : base_ltype s * type.for_each_lhs_of_arrow _ d) => |
| 30 |
translate_func' (f (fst args)) nextn (snd args) |
| 31 |
(* if any expression that outputs a base type, call translate_cmd *) |
| 32 |
| expr.Ident (type.base b) idc => |
| 33 |
fun (_:unit) => translate_cmd (expr.Ident idc) nextn |
| 34 |
| expr.Var (type.base b) v => |
| 35 |
fun (_:unit) => translate_cmd (expr.Var v) nextn |
| 36 |
| expr.App _ (type.base b) f x => |
| 37 |
fun (_:unit) => translate_cmd (expr.App f x) nextn |
| 38 |
| expr.LetIn _ (type.base b) x f => |
| 39 |
fun (_:unit) => translate_cmd (expr.LetIn x f) nextn |
| 40 |
(* if the expression does not have a base type and is not an Abs, |
| 41 |
return garbage *) |
| 42 |
| _ => fun _ => (0%nat, dummy_base_ltype _, cmd.skip) |
| 43 |
end. |
| 44 |
|
| 45 |
(* Translates functions in 3 steps: |
| 46 |
1) load any lists from the arguments |
| 47 |
2) call translate_cmd to translate the function body and get the |
| 48 |
return values |
| 49 |
3) store the return values in the designated variables (with |
| 50 |
lists being written into memory) |
| 51 |
|
| 52 |
The reason for the load/translate/store pattern is so that, for |
| 53 |
the inductive proof of translate_cmd, there is no need to reason |
| 54 |
about the memory. Since fiat-crypto doesn't do any list |
| 55 |
manipulations in the middle of functions, but only uses lists in |
| 56 |
arguments/return values, it's a convenient formalization. *) |
| 57 |
Definition translate_func {t} |
| 58 |
(e : API.Expr t) |
| 59 |
(* argument variables *) |
| 60 |
(argnames : type.for_each_lhs_of_arrow ltype t) |
| 61 |
(* lengths of argument lists *) |
| 62 |
(lengths : type.for_each_lhs_of_arrow list_lengths t) |
| 63 |
(* integer sizes of argument lists *) |
| 64 |
(argsizes : type.for_each_lhs_of_arrow access_sizes t) |
| 65 |
(* return variables *) |
| 66 |
(rets : base_ltype (type.final_codomain t)) |
| 67 |
(* integer sizes of return lists *) |
| 68 |
(retsizes : base_access_sizes (type.final_codomain t)) |
| 69 |
: list string * list string * cmd (* bedrock function *) |
| 70 |
* list_lengths (type.base (type.final_codomain t)) (* output list lengths *) := |
| 71 |
(* load arguments *) |
| 72 |
let load_args_out := load_arguments 0%nat argnames lengths argsizes in |
| 73 |
let nextn := fst (fst load_args_out) in |
| 74 |
let args := snd (fst load_args_out) in |
| 75 |
let load_args_cmd := snd load_args_out in |
| 76 |
(* translate *) |
| 77 |
let out := translate_func' (e _) nextn args in |
| 78 |
(* store return values *) |
| 79 |
let store_rets := store_return_values (snd (fst out)) rets retsizes in |
| 80 |
(* make new arguments for pointers to returned lists *) |
| 81 |
let part := extract_listnames rets in |
| 82 |
let out_ptrs := flatten_listonly_base_ltype (fst part) in |
| 83 |
let innames := out_ptrs ++ flatten_argnames argnames in |
| 84 |
let outnames := flatten_listexcl_base_ltype (snd part) in |
| 85 |
(* assemble executable body: load arguments, function body, store rets *) |
| 86 |
let body := cmd.seq (cmd.seq load_args_cmd (snd out)) (snd store_rets) in |
| 87 |
(* assemble func (arg varnames, return varnames, executable body), |
| 88 |
also add in output list lengths *) |
| 89 |
(innames, outnames, body, fst store_rets). |
| 90 |
End Func. |