@@ -63,6 +63,17 @@ type malfunction_plugin_config =
6363let debug_extract = CDebug. create ~name: " verified-extraction" ()
6464let debug = debug_extract
6565
66+ let get_bool_option key =
67+ let open Goptions in
68+ match get_option_value key with
69+ | Some get -> fun () ->
70+ begin match get () with
71+ | BoolValue b -> b
72+ | _ -> assert false
73+ end
74+ | None ->
75+ (declare_bool_option_and_ref ~key ~value: false () ).get
76+
6677let get_stringopt_option key =
6778 let open Goptions in
6879 match get_option_value key with
@@ -80,6 +91,21 @@ let get_build_dir_opt =
8091let get_opam_path_opt =
8192 get_stringopt_option [" Verified" ; " Extraction" ; " Opam" ; " Path" ]
8293
94+ let get_timing_opt =
95+ get_bool_option [" Verified" ; " Extraction" ; " Timing" ]
96+
97+ let get_typed_opt =
98+ get_bool_option [" Verified" ; " Extraction" ; " Typed" ]
99+
100+ let get_verbose_opt =
101+ get_bool_option [" Verified" ; " Extraction" ; " Verbose" ]
102+
103+ let get_optimize_opt =
104+ get_bool_option [" Verified" ; " Extraction" ; " Optimize" ]
105+
106+ let get_format_opt =
107+ get_bool_option [" Verified" ; " Extraction" ; " Format" ]
108+
83109(* When building standalone programs still relying on Rocq's/MetaRocq's FFIs, use these packages for linking *)
84110let statically_linked_pkgs =
85111 [" rocq-runtime.boot" ;
@@ -280,8 +306,10 @@ let make_options loc l =
280306 let prims = get_global_prims () in
281307 let default = {
282308 malfunction_pipeline_config = default_malfunction_config inductives_mapping inlining prims;
283- bypass_qeds = false ; time = false ; program_type = None ; load = false ; run = false ;
284- verbose = false ; loc; format = false ; optimize = false }
309+ bypass_qeds = false ; time = get_timing_opt () ;
310+ program_type = None ; load = false ; run = false ;
311+ verbose = get_verbose_opt () ; loc; format = get_format_opt () ;
312+ optimize = get_optimize_opt () }
285313 in
286314 let parse_unsafe_flags unsafe l =
287315 match l with
@@ -421,17 +449,16 @@ type malfunction_program_type =
421449
422450type plugin_function = Obj .t
423451
424- let register_plugins = Summary. ref ~name: " verified-extraction-plugins" (CString.Map. empty : plugin_function CString.Map.t )
452+ let register_plugins = Summary. ref ~local: true ~ name:" verified-extraction-plugins" (CString.Map. empty : plugin_function CString.Map.t )
425453
426454let cache_plugin (name , fn ) =
427455 register_plugins := CString.Map. add name fn ! register_plugins
428456
429457let plugin_input =
430458 let open Libobject in
431459 declare_object
432- (global_object_nodischarge " verified-extraction-plugins"
433- ~cache: (fun r -> cache_plugin r)
434- ~subst: None )
460+ (local_object_nodischarge " verified-extraction-plugins"
461+ ~cache: (fun r -> cache_plugin r))
435462
436463let register_plugin name fn : unit =
437464 Lib. add_leaf (plugin_input (name, fn))
@@ -612,8 +639,7 @@ struct
612639
613640 let reify opts env sigma tyinfo result =
614641 let mapping = opts.malfunction_pipeline_config.reorder_constructors in
615- if opts.time then time opts (Pp. str " Reification" ) (reify env sigma mapping tyinfo) result
616- else reify env sigma mapping tyinfo result
642+ time opts (Pp. str " Reification" ) (reify env sigma mapping tyinfo) result
617643
618644end
619645
@@ -677,12 +703,15 @@ let compile opts names tyinfos fname =
677703 notice opts Pp. (fun () -> str " Compiled to " ++ str output);
678704 Some (StandaloneProgram output)
679705
680- let run_code opts env sigma tyinfo code : Constr.t =
706+ let run_code opts env sigma fn tyinfo code : Constr.t =
681707 let open Reify in
682708 let value, tyinfo =
683- match tyinfo with
684- | IsThunk vty -> ((Obj. magic code : unit -> Obj.t ) () , vty)
685- | IsValue vty -> code, vty
709+ try
710+ match tyinfo with
711+ | IsThunk vty -> (time opts Pp. (str fn) (Obj. magic code : unit -> Obj.t ) () , vty)
712+ | IsValue vty -> time opts Pp. (str fn) (fun _ -> code) () , vty
713+ with Stack_overflow ->
714+ CErrors. user_err Pp. (str" Compiled program has overflown the stack." )
686715 in
687716 Reify. reify opts env sigma tyinfo value
688717
@@ -696,7 +725,7 @@ let run opts env sigma result : Constr.t list option =
696725 let run fn tyinfo =
697726 match CString.Map. find_opt fn ! register_plugins with
698727 | None -> CErrors. anomaly Pp. (str" Couldn't find funtion " ++ str fn ++ str" which should have been registered by " ++ str shared_lib)
699- | Some code -> time opts Pp. (str fn) ( run_code opts env sigma tyinfo) code
728+ | Some code -> run_code opts env sigma fn tyinfo code
700729 in
701730 Some (List. map2 run fns tyinfos)
702731 end else None
@@ -803,7 +832,7 @@ let eval_plugin_gen ?loc opts (gr : Libnames.qualid) =
803832 let sigma, grc = Evd. fresh_global env sigma gr in
804833 let tyinfo = Reify. check_reifyable_thunk_or_value env sigma grc in
805834 let code = eval_name fn in
806- let c = run_code opts env sigma tyinfo code in
835+ let c = run_code opts env sigma fn tyinfo code in
807836 env, sigma, c
808837
809838let eval_plugin ?loc opts (gr : Libnames.qualid ) =
@@ -816,15 +845,23 @@ let eval ?loc opts gr =
816845
817846let prc = Printer. pr_constr_env
818847
848+ let make_thunk term ty =
849+ let unit = EConstr. mkIndU (Globnames. destIndRef (Rocqlib. lib_ref " core.unit.type" ), EConstr.EInstance. empty) in
850+ let binder = EConstr. anonR in
851+ let term = EConstr. mkLambda (binder, unit , term) in
852+ let ty = EConstr. mkProd (binder, unit , ty) in
853+ term, ty
854+
819855let erase_eval_function compile_malfunction : Eraseconv.erase_evaluation_function =
856+ let opts = [ProgramType Plugin ; Load ; Run ] in
820857 fun env term ty ->
858+ let opts = make_options None opts in
821859 let sigma = Evd. from_env env in
822860 debug Pp. (fun () -> str" Erasure evaluation called on term: " ++ prc env sigma term ++ str" of type " ++
823861 prc env sigma ty);
824- let opts = [ProgramType Plugin ; Time ; Load ; Run ] in
825- let opts = make_options None opts in
826- let tyinfo = time opts Pp. (str" Checking reifyability" ) (Reify. check_reifyable_thunk_or_value_type env sigma) (EConstr. of_constr ty) in
827- let res = time opts Pp. (str" Extracting, running and reifying" ) (extract_and_run compile_malfunction opts env sigma (EConstr. of_constr term) [tyinfo]) None in
862+ let term, ty = make_thunk (EConstr. of_constr term) (EConstr. of_constr ty) in
863+ let tyinfo = time opts Pp. (str" Checking reifyability" ) (Reify. check_reifyable_thunk_or_value_type env sigma) ty in
864+ let res = time opts Pp. (str" Extracting, running and reifying" ) (extract_and_run compile_malfunction opts env sigma term [tyinfo]) None in
828865 match res with
829866 | None ->
830867 debug Pp. (fun () -> str" Didn't reify to a value" );
0 commit comments