From 5df005f4b7ab929d48bdc48175ebd62b008427f3 Mon Sep 17 00:00:00 2001 From: Tom Jack Date: Tue, 21 Apr 2020 13:28:39 +0000 Subject: [PATCH] Add `print-mini-c --optimize=entrypoint` to print optimized mini_c --- src/bin/cli.ml | 18 +++++++++++++++--- src/main/compile/of_mini_c.ml | 14 +++++++++++++- 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/src/bin/cli.ml b/src/bin/cli.ml index dec0ac0bf..540b07f12 100644 --- a/src/bin/cli.ml +++ b/src/bin/cli.ml @@ -224,15 +224,27 @@ let print_ast_typed = let doc = "Subcommand: Print the typed AST.\n Warning: Intended for development of LIGO and can break at any time." in (Term.ret term, Term.info ~doc cmdname) +let optimize = + let open Arg in + let docv = "ENTRY_POINT" in + let doc = "Apply Mini-C optimizations as if compiling $(docv)" in + let info = + info ~docv ~doc ["optimize"] in + value @@ opt (some string) None info + let print_mini_c = - let f source_file syntax display_format = ( + let f source_file syntax display_format optimize = ( toplevel ~display_format @@ let%bind typed,_ = Compile.Utils.type_file source_file syntax Env in let%bind mini_c = Compile.Of_typed.compile typed in - ok @@ Format.asprintf "%a\n" Compile.Of_mini_c.pretty_print mini_c + match optimize with + | None -> ok @@ Format.asprintf "%a\n" Compile.Of_mini_c.pretty_print mini_c + | Some entry_point -> + let%bind mini_c = Compile.Of_mini_c.aggregate_contract mini_c entry_point in + ok @@ Format.asprintf "%a\n" Mini_c.PP.expression mini_c ) in - let term = Term.(const f $ source_file 0 $ syntax $ display_format) in + let term = Term.(const f $ source_file 0 $ syntax $ display_format $ optimize) in let cmdname = "print-mini-c" in let doc = "Subcommand: Print Mini-C. Warning: Intended for development of LIGO and can break at any time." in (Term.ret term, Term.info ~doc cmdname) diff --git a/src/main/compile/of_mini_c.ml b/src/main/compile/of_mini_c.ml index 8b95d9a2d..df000bb54 100644 --- a/src/main/compile/of_mini_c.ml +++ b/src/main/compile/of_mini_c.ml @@ -33,4 +33,16 @@ let aggregate_and_compile_expression = fun program exp -> aggregate_and_compile program (ExpressionForm exp) let pretty_print program = - Mini_c.PP.program program \ No newline at end of file + Mini_c.PP.program program + + +(* TODO refactor? *) + +let aggregate = fun program form -> + let%bind aggregated = aggregate_entry program form in + ok @@ Self_mini_c.all_expression aggregated + +let aggregate_contract = fun (program : Types.program) name -> + let%bind (exp, idx) = get_entry program name in + let program' = List.take idx program in + aggregate program' (ContractForm exp)