text
stringlengths 12
786k
|
---|
let make_printable node = Micheline_printer . printable Michelson_v1_primitives . string_of_prim node
|
let pp_error_kind fmtr ( error_kind : error_kind ) = match error_kind with | Global_error { benchmark_name ; workload } -> Format . open_vbox 1 ; Format . fprintf fmtr " Global error , " :@ ; Format . fprintf fmtr " benchmark = % s , " @ benchmark_name ; Format . fprintf fmtr " workload , " :@ ; report_michelson_errors fmtr workload ; Format . close_box ( ) | Bad_data { benchmark_name ; micheline ; expected_type ; phase } -> Format . open_vbox 1 ; Format . fprintf fmtr " Bad data , " :@ ; Format . fprintf fmtr " benchmark = % s , " @ benchmark_name ; Format . fprintf fmtr " expression = [ @< v 1 >% a ] , " @@ Micheline_printer . print_expr ( make_printable micheline ) ; Format . fprintf fmtr " expected type = [ @< v 1 >% a ] , " @@ Micheline_printer . print_expr ( make_printable expected_type ) ; Format . fprintf fmtr " phase = % a , " @ pp_phase phase ; Format . close_box ( ) | Bad_code { benchmark_name ; micheline ; expected_stack_type ; phase } -> Format . open_vbox 1 ; Format . fprintf fmtr " Bad code , " :@ ; Format . fprintf fmtr " benchmark = % s , " @ benchmark_name ; Format . fprintf fmtr " expression = [ @< v 1 >% a ] , " @@ Micheline_printer . print_expr ( make_printable micheline ) ; Format . fprintf fmtr " expected stack = [ @< v 1 >% a ] , " @@ ( Format . pp_print_list ~ pp_sep ( : fun fmtr ( ) -> Format . fprintf fmtr " " ) :: ( fun fmtr node -> let printable = make_printable node in Format . fprintf fmtr " % a " Micheline_printer . print_expr printable ) ) expected_stack_type ; Format . fprintf fmtr " phase = % a , " @ pp_phase phase ; Format . close_box ( )
|
let ( ) = Printexc . register_printer ( function | Translator_benchmark_error err -> Some ( Format . asprintf " % a " pp_error_kind err ) | _ -> None )
|
let global_error benchmark_name workload = raise ( Translator_benchmark_error ( Global_error { benchmark_name ; workload } ) )
|
let bad_data benchmark_name micheline expected_type phase = raise ( Translator_benchmark_error ( Bad_data { benchmark_name ; micheline ; expected_type ; phase } ) )
|
let bad_code benchmark_name micheline expected_stack_type phase = raise ( Translator_benchmark_error ( Bad_code { benchmark_name ; micheline ; expected_stack_type ; phase } ) )
|
module Typechecking_data : Benchmark . S = struct include Config include Default_boilerplate let models = make_models Translator_workload . Parsing Translator_workload . Data let name = " TYPECHECKING_DATA " let info = " Benchmarking typechecking of data " let typechecking_data_benchmark rng_state ( node : Protocol . Script_repr . expr ) ( michelson_type : Script_repr . expr ) = Lwt_main . run ( Execution_context . make ~ rng_state >>=? fun ( ctxt , _ ) -> let ex_ty = Type_helpers . michelson_type_to_ex_ty michelson_type ctxt in let workload = match Translator_workload . data_typechecker_workload ctxt Translator_workload . Parsing ( Micheline . root node ) ex_ty with | None -> bad_data name node michelson_type Workload_production | Some workload -> workload in match ex_ty with | Script_ir_translator . Ex_ty ty -> let closure ( ) = match Lwt_main . run ( Script_ir_translator . parse_data ctxt ~ legacy : false ~ allow_forged : false ty ( Micheline . root node ) ) with | Error _ | ( exception _ ) -> bad_data name node michelson_type In_protocol | Ok _ -> ( ) in return ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let make_bench rng_state cfg ( ) = let Michelson_mcmc_samplers . { term ; typ } = Michelson_generation . make_data_sampler rng_state cfg . generator_config in typechecking_data_benchmark rng_state term typ let create_benchmarks ~ rng_state ~ bench_num config = match config . michelson_terms_file with | Some file -> Format . eprintf " Loading terms from % s . " @ file ; let terms = Michelson_mcmc_samplers . load ~ filename : file in List . filter_map ( function | Michelson_mcmc_samplers . Data { term ; typ } -> Some ( fun ( ) -> typechecking_data_benchmark rng_state term typ ) | _ -> None ) terms | None -> Format . eprintf " No michelson_terms_file given , generating on - the - fly . " @ ; List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Typechecking_data )
|
module Unparsing_data : Benchmark . S = struct include Config include Default_boilerplate let models = make_models Translator_workload . Unparsing Translator_workload . Data let name = " UNPARSING_DATA " let info = " Benchmarking unparsing of data " let unparsing_data_benchmark rng_state ( node : Protocol . Script_repr . expr ) ( michelson_type : Protocol . Script_repr . expr ) = Lwt_main . run ( Execution_context . make ~ rng_state >>=? fun ( ctxt , _ ) -> let ex_ty = Type_helpers . michelson_type_to_ex_ty michelson_type ctxt in let workload = match Translator_workload . data_typechecker_workload ctxt Translator_workload . Unparsing ( Micheline . root node ) ex_ty with | None -> bad_data name node michelson_type Workload_production | Some workload -> workload in match ex_ty with | Script_ir_translator . Ex_ty ty -> Script_ir_translator . parse_data ctxt ~ legacy : false ~ allow_forged : false ty ( Micheline . root node ) >|= Environment . wrap_tzresult >>=? fun ( typed , ctxt ) -> let closure ( ) = match Lwt_main . run ( Script_ir_translator . unparse_data ctxt Script_ir_translator . Optimized ty typed ) with | Error _ | ( exception _ ) -> bad_data name node michelson_type In_protocol | Ok _ -> ( ) in return ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let make_bench rng_state cfg ( ) = let Michelson_mcmc_samplers . { term ; typ } = Michelson_generation . make_data_sampler rng_state cfg . generator_config in unparsing_data_benchmark rng_state term typ let create_benchmarks ~ rng_state ~ bench_num config = match config . michelson_terms_file with | Some file -> Format . eprintf " Loading terms from % s . " @ file ; let terms = Michelson_mcmc_samplers . load ~ filename : file in List . filter_map ( function | Michelson_mcmc_samplers . Data { term ; typ } -> Some ( fun ( ) -> unparsing_data_benchmark rng_state term typ ) | _ -> None ) terms | None -> Format . eprintf " No michelson_terms_file given , generating on - the - fly . " @ ; List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Unparsing_data )
|
module Typechecking_code : Benchmark . S = struct include Config include Default_boilerplate let models = make_models Translator_workload . Parsing Translator_workload . Code let name = " TYPECHECKING_CODE " let info = " Benchmarking typechecking of code " let typechecking_code_benchmark rng_state ( node : Protocol . Script_repr . expr ) ( stack : Script_repr . expr list ) = Lwt_main . run ( Execution_context . make ~ rng_state >>=? fun ( ctxt , _ ) -> let ex_stack_ty = Type_helpers . michelson_type_list_to_ex_stack_ty stack ctxt in let workload = match Translator_workload . code_typechecker_workload ctxt Translator_workload . Parsing ( Micheline . root node ) ex_stack_ty with | None -> bad_code name node stack Workload_production | Some workload -> workload in let ( Script_ir_translator . Ex_stack_ty bef ) = ex_stack_ty in let closure ( ) = let result = Lwt_main . run ( Script_ir_translator . parse_instr Script_tc_context . data ctxt ~ legacy : false ( Micheline . root node ) bef ) in match Environment . wrap_tzresult result with | Error errs -> Format . eprintf " % a . " @ Error_monad . pp_print_trace errs ; bad_code name node stack In_protocol | Ok _ -> ( ) in return ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let make_bench rng_state ( cfg : Config . config ) ( ) = let open Michelson_generation in let Michelson_mcmc_samplers . { term ; bef ; aft = _ } = make_code_sampler rng_state cfg . generator_config in typechecking_code_benchmark rng_state term bef let create_benchmarks ~ rng_state ~ bench_num config = match config . michelson_terms_file with | Some file -> Format . eprintf " Loading terms from % s . " @ file ; let terms = Michelson_mcmc_samplers . load ~ filename : file in List . filter_map ( function | Michelson_mcmc_samplers . Code { term ; bef ; aft = _ } -> Some ( fun ( ) -> typechecking_code_benchmark rng_state term bef ) | _ -> None ) terms | None -> Format . eprintf " No michelson_terms_file given , generating on - the - fly . " @ ; List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Typechecking_code )
|
module Unparsing_code : Benchmark . S = struct include Config include Default_boilerplate let models = make_models Translator_workload . Unparsing Translator_workload . Code let name = " UNPARSING_CODE " let info = " Benchmarking unparsing of code " let unparsing_code_benchmark rng_state ( node : Protocol . Script_repr . expr ) ( stack : Script_repr . expr list ) = Lwt_main . run ( Execution_context . make ~ rng_state >>=? fun ( ctxt , _ ) -> let ex_stack_ty = Type_helpers . michelson_type_list_to_ex_stack_ty stack ctxt in let workload = match Translator_workload . code_typechecker_workload ctxt Translator_workload . Unparsing ( Micheline . root node ) ex_stack_ty with | None -> bad_code name node stack Workload_production | Some workload -> workload in let ( Script_ir_translator . Ex_stack_ty bef ) = ex_stack_ty in Script_ir_translator . parse_instr Script_tc_context . data ctxt ~ legacy : false ( Micheline . root node ) bef >|= Environment . wrap_tzresult >>=? fun ( _typed , ctxt ) -> let closure ( ) = let result = Lwt_main . run ( Script_ir_translator . unparse_code ctxt Optimized ( Micheline . root node ) ) in match Environment . wrap_tzresult result with | Error errs -> Format . eprintf " % a . " @ Error_monad . pp_print_trace errs ; bad_code name node stack In_protocol | Ok _ -> ( ) in return ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let make_bench rng_state ( cfg : Config . config ) ( ) = let open Michelson_generation in let Michelson_mcmc_samplers . { term ; bef ; aft = _ } = make_code_sampler rng_state cfg . generator_config in unparsing_code_benchmark rng_state term bef let create_benchmarks ~ rng_state ~ bench_num config = match config . michelson_terms_file with | Some file -> Format . eprintf " Loading terms from % s . " @ file ; let terms = Michelson_mcmc_samplers . load ~ filename : file in List . filter_map ( function | Michelson_mcmc_samplers . Code { term ; bef ; aft = _ } -> Some ( fun ( ) -> unparsing_code_benchmark rng_state term bef ) | _ -> None ) terms | None -> List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Unparsing_code )
|
let rec check_printable_ascii v i = if Compare . Int . ( i < 0 ) then true else match v . [ i ] with | ' \ n ' | ' \ x20 ' . . ' \ x7E ' -> check_printable_ascii v ( i - 1 ) | _ -> false
|
let check_printable_benchmark = let open Tezos_shell_benchmarks . Encoding_benchmarks_helpers in linear_shared ~ name " : CHECK_PRINTABLE " ~ generator ( : fun rng_state -> let open Base_samplers in let string = readable_ascii_string rng_state ~ size { : min = 1 ; max = 1024 } in ( string , { Shared_linear . bytes = String . length string } ) ) ~ make_bench ( : fun generator ( ) -> let ( generated , workload ) = generator ( ) in let closure ( ) = ignore ( check_printable_ascii generated ( String . length generated - 1 ) ) in Generator . Plain { workload ; closure } )
|
let ( ) = Registration_helpers . register check_printable_benchmark
|
module Merge_types : Benchmark . S = struct type config = { max_size : int } let config_encoding = let open Data_encoding in conv ( fun { max_size } -> max_size ) ( fun max_size -> { max_size } ) ( obj1 ( req " max_size " int31 ) ) let default_config = { max_size = 64 } type workload = Merge_types_workload of { nodes : int ; consumed : Size . t } let workload_encoding = let open Data_encoding in conv ( function Merge_types_workload { nodes ; consumed } -> ( nodes , consumed ) ) ( fun ( nodes , consumed ) -> Merge_types_workload { nodes ; consumed } ) ( obj2 ( req " nodes " int31 ) ( req " consumed " int31 ) ) let workload_to_vector = function | Merge_types_workload { nodes ; consumed } -> Sparse_vec . String . of_list [ ( " nodes " , float_of_int nodes ) ; ( " consumed " , float_of_int consumed ) ] let name = " MERGE_TYPES " let info = " Benchmarking merging of types " let tags = [ Tags . translator ] let intercept_var = Free_variable . of_string ( Format . asprintf " % s_const " name ) let coeff_var = Free_variable . of_string ( Format . asprintf " % s_coeff " name ) let size_model = Model . make ~ conv ( : function Merge_types_workload { nodes ; _ } -> ( nodes , ( ) ) ) ~ model : ( Model . affine_split_const ~ intercept1 : Builtin_benchmarks . timer_variable ~ intercept2 : intercept_var ~ coeff : coeff_var ) let codegen_model = Model . make ~ conv ( : function Merge_types_workload { nodes ; _ } -> ( nodes , ( ) ) ) ~ model ( : Model . affine ~ intercept : intercept_var ~ coeff : coeff_var ) let ( ) = Registration_helpers . register_for_codegen name ( Model . For_codegen codegen_model ) let models = [ ( " size_translator_model " , size_model ) ; ( " codegen " , codegen_model ) ] let merge_type_benchmark rng_state nodes ( ty : Script_ir_translator . ex_ty ) = let open Error_monad in Lwt_main . run ( Execution_context . make ~ rng_state >>=? fun ( ctxt , _ ) -> let ctxt = Gas_helpers . set_limit ctxt in match ty with | Ex_ty ty -> let dummy_loc = 0 in Lwt . return ( Script_ir_translator . ty_eq ctxt dummy_loc ty ty ) >|= Environment . wrap_tzresult >>=? fun ( _ , ctxt ' ) -> let consumed = Alpha_context . Gas . consumed ~ since : ctxt ~ until : ctxt ' in let workload = Merge_types_workload { nodes ; consumed = Z . to_int ( Gas_helpers . fp_to_z consumed ) } in let closure ( ) = ignore ( Script_ir_translator . ty_eq ctxt 0 ty ty ) in return ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let make_bench rng_state ( cfg : config ) ( ) = let nodes = Base_samplers . ( sample_in_interval ~ range { : min = 1 ; max = cfg . max_size } rng_state ) in let ty = Michelson_generation . Samplers . Random_type . m_type ~ size : nodes rng_state in merge_type_benchmark rng_state nodes ty let create_benchmarks ~ rng_state ~ bench_num config = List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Merge_types )
|
let rec dummy_type_generator size = let open Script_ir_translator in let open Script_typed_ir in if size <= 1 then Ex_ty unit_t else match dummy_type_generator ( size - 2 ) with | Ex_ty r -> let l = unit_t in Ex_ty ( match pair_t ( - 1 ) l r with Error _ -> assert false | Ok t -> t )
|
let rec dummy_comparable_type_generator size = let open Script_ir_translator in let open Script_typed_ir in if size <= 0 then Ex_comparable_ty unit_key else match dummy_comparable_type_generator ( size - 2 ) with | Ex_comparable_ty r -> let l = unit_key in Ex_comparable_ty ( match pair_key ( - 1 ) l r with Error _ -> assert false | Ok t -> t )
|
module Parse_type_shared = struct type config = { max_size : int } let default_config = { max_size = Constants_repr . michelson_maximum_type_size } let config_encoding = let open Data_encoding in conv ( fun { max_size } -> max_size ) ( fun max_size -> { max_size } ) ( obj1 ( req " max_size " int31 ) ) type workload = Type_workload of { nodes : int ; consumed : Size . t } let workload_encoding = let open Data_encoding in conv ( function Type_workload { nodes ; consumed } -> ( nodes , consumed ) ) ( fun ( nodes , consumed ) -> Type_workload { nodes ; consumed } ) ( obj2 ( req " nodes " int31 ) ( req " consumed " int31 ) ) let workload_to_vector = function | Type_workload { nodes ; consumed } -> Sparse_vec . String . of_list [ ( " nodes " , float_of_int nodes ) ; ( " consumed " , float_of_int consumed ) ] let tags = [ Tags . translator ] end
|
let parse_ty ctxt node = Script_ir_translator . parse_ty ctxt ~ legacy : true ~ allow_lazy_storage : true ~ allow_operation : true ~ allow_contract : true ~ allow_ticket : true node
|
let unparse_ty ctxt ty = Script_ir_translator . unparse_ty ~ loc ( :- 1 ) ctxt ty
|
module Parse_type_benchmark : Benchmark . S = struct include Parse_type_shared let name = " PARSE_TYPE " let info = " Benchmarking parse_ty " let make_bench rng_state config ( ) = let open Error_monad in ( Lwt_main . run ( Execution_context . make ~ rng_state ) >>? fun ( ctxt , _ ) -> let ctxt = Gas_helpers . set_limit ctxt in let size = Random . State . int rng_state config . max_size in let ty = dummy_type_generator size in match ty with | Ex_ty ty -> Environment . wrap_tzresult @@ unparse_ty ctxt ty >>? fun ( unparsed , _ ) -> Environment . wrap_tzresult @@ parse_ty ctxt unparsed >>? fun ( _ , ctxt ' ) -> let consumed = Z . to_int ( Gas_helpers . fp_to_z ( Alpha_context . Gas . consumed ~ since : ctxt ~ until : ctxt ' ) ) in let nodes = let x = Script_typed_ir . ty_size ty in Saturation_repr . to_int @@ Script_typed_ir . Type_size . to_int x in let workload = Type_workload { nodes ; consumed } in let closure ( ) = ignore ( parse_ty ctxt unparsed ) in ok ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let size_model = Model . make ~ conv ( : function Type_workload { nodes ; consumed = _ } -> ( nodes , ( ) ) ) ~ model : ( Model . affine ~ intercept : ( Free_variable . of_string ( Format . asprintf " % s_const " name ) ) ~ coeff ( : Free_variable . of_string ( Format . asprintf " % s_coeff " name ) ) ) let models = [ ( " size_translator_model " , size_model ) ] let create_benchmarks ~ rng_state ~ bench_num config = List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Parse_type_benchmark )
|
module Unparse_type_benchmark : Benchmark . S = struct include Parse_type_shared let name = " UNPARSE_TYPE " let info = " Benchmarking unparse_ty " let make_bench rng_state config ( ) = let open Error_monad in ( Lwt_main . run ( Execution_context . make ~ rng_state ) >>? fun ( ctxt , _ ) -> let ctxt = Gas_helpers . set_limit ctxt in let size = Random . State . int rng_state config . max_size in let ty = dummy_type_generator size in match ty with | Ex_ty ty -> Environment . wrap_tzresult @@ unparse_ty ctxt ty >>? fun ( _ , ctxt ' ) -> let consumed = Z . to_int ( Gas_helpers . fp_to_z ( Alpha_context . Gas . consumed ~ since : ctxt ~ until : ctxt ' ) ) in let nodes = let x = Script_typed_ir . ty_size ty in Saturation_repr . to_int @@ Script_typed_ir . Type_size . to_int x in let workload = Type_workload { nodes ; consumed } in let closure ( ) = ignore ( unparse_ty ctxt ty ) in ok ( Generator . Plain { workload ; closure } ) ) |> function | Ok closure -> closure | Error errs -> global_error name errs let size_model = Model . make ~ conv ( : function Type_workload { nodes ; consumed = _ } -> ( nodes , ( ) ) ) ~ model : ( Model . affine ~ intercept : ( Free_variable . of_string ( Format . asprintf " % s_const " name ) ) ~ coeff ( : Free_variable . of_string ( Format . asprintf " % s_coeff " name ) ) ) let models = [ ( " size_translator_model " , size_model ) ] let create_benchmarks ~ rng_state ~ bench_num config = List . repeat bench_num ( make_bench rng_state config ) let ( ) = Registration_helpers . register_for_codegen name ( Model . For_codegen size_model ) end
|
let ( ) = Registration_helpers . register ( module Unparse_type_benchmark )
|
module Unparse_comparable_type_benchmark : Benchmark . S = struct include Parse_type_shared let name = " UNPARSE_COMPARABLE_TYPE " let info = " Benchmarking unparse_comparable_ty " let make_bench rng_state config ( ) = let open Error_monad in let res = Lwt_main . run ( Execution_context . make ~ rng_state ) >>? fun ( ctxt , _ ) -> let ctxt = Gas_helpers . set_limit ctxt in let size = Random . State . int rng_state config . max_size in let ty = dummy_comparable_type_generator size in let nodes = let ( Script_ir_translator . Ex_comparable_ty ty ) = ty in let x = Script_typed_ir . comparable_ty_size ty in Saturation_repr . to_int @@ Script_typed_ir . Type_size . to_int x in match ty with | Ex_comparable_ty comp_ty -> Environment . wrap_tzresult @@ Script_ir_translator . unparse_comparable_ty ~ loc ( ) : ctxt comp_ty >>? fun ( _ , ctxt ' ) -> let consumed = Z . to_int ( Gas_helpers . fp_to_z ( Alpha_context . Gas . consumed ~ since : ctxt ~ until : ctxt ' ) ) in let workload = Type_workload { nodes ; consumed } in let closure ( ) = ignore ( Script_ir_translator . unparse_comparable_ty ~ loc ( ) : ctxt comp_ty ) in ok ( Generator . Plain { workload ; closure } ) in match res with | Ok closure -> closure | Error errs -> global_error name errs let size_model = Model . make ~ conv ( : function Type_workload { nodes ; consumed = _ } -> ( nodes , ( ) ) ) ~ model : ( Model . affine ~ intercept : ( Free_variable . of_string ( Format . asprintf " % s_const " name ) ) ~ coeff ( : Free_variable . of_string ( Format . asprintf " % s_coeff " name ) ) ) let ( ) = Registration_helpers . register_for_codegen name ( Model . For_codegen size_model ) let models = [ ( " size_translator_model " , size_model ) ] let create_benchmarks ~ rng_state ~ bench_num config = List . repeat bench_num ( make_bench rng_state config ) end
|
let ( ) = Registration_helpers . register ( module Unparse_comparable_type_benchmark )
|
let gas_full t_kind code_or_data = let name = Format . asprintf " % a_ % a " Translator_workload . pp_kind t_kind Translator_workload . pp_code_or_data code_or_data in let intercept = Free_variable . of_string ( Format . asprintf " % s_const " name ) in let coeff = Free_variable . of_string ( Format . asprintf " % s_coeff " name ) in Model . affine ~ intercept ~ coeff
|
let size_full t_kind code_or_data = let name = Format . asprintf " % a_ % a " Translator_workload . pp_kind t_kind Translator_workload . pp_code_or_data code_or_data in let coeff1 = Free_variable . of_string ( Format . asprintf " % s_traversal " name ) in let coeff2 = Free_variable . of_string ( Format . asprintf " % s_int_bytes " name ) in let coeff3 = Free_variable . of_string ( Format . asprintf " % s_string_bytes " name ) in Model . trilinear ~ coeff1 ~ coeff2 ~ coeff3
|
let gas_based_model t_kind code_or_data = Model . make ~ conv ( : function | Translator_workload . Typechecker_workload { consumed ; _ } -> ( consumed , ( ) ) ) ~ model ( : gas_full t_kind code_or_data )
|
let size_based_model t_kind code_or_data = Model . make ~ conv ( : function | Translator_workload . Typechecker_workload { micheline_size ; _ } -> ( match micheline_size with | { traversal ; int_bytes ; string_bytes } -> ( traversal , ( int_bytes , ( string_bytes , ( ) ) ) ) ) ) ~ model ( : size_full t_kind code_or_data )
|
type kind = Parsing | Unparsing
|
type code_or_data = Code | Data
|
type t = | Typechecker_workload of { t_kind : kind ; code_or_data : code_or_data ; micheline_size : Size . micheline_size ; consumed : Size . t ; }
|
let kind_encoding : kind Data_encoding . t = let open Data_encoding in def " kind_encoding " @@ string_enum [ ( " parsing " , Parsing ) ; ( " unparsing " , Unparsing ) ]
|
let code_or_data_encoding : code_or_data Data_encoding . t = let open Data_encoding in def " code_or_data_encoding " @@ string_enum [ ( " code " , Code ) ; ( " data " , Data ) ]
|
let encoding : t Data_encoding . t = let open Data_encoding in def " translator_trace_encoding " @@ conv ( function | Typechecker_workload { t_kind ; code_or_data ; micheline_size ; consumed } -> ( t_kind , code_or_data , micheline_size , consumed ) ) ( fun ( t_kind , code_or_data , micheline_size , consumed ) -> Typechecker_workload { t_kind ; code_or_data ; micheline_size ; consumed } ) ( tup4 kind_encoding code_or_data_encoding Size . micheline_size_encoding Size . encoding )
|
let pp_kind fmtr ( kind : kind ) = match kind with | Parsing -> Format . pp_print_string fmtr " Parsing " | Unparsing -> Format . pp_print_string fmtr " Unparsing "
|
let pp_code_or_data fmtr ( x : code_or_data ) = match x with | Code -> Format . pp_print_string fmtr " Code " | Data -> Format . pp_print_string fmtr " Data "
|
let pp fmtr ( trace : t ) = match trace with | Typechecker_workload { t_kind ; code_or_data ; micheline_size ; consumed } -> Format . fprintf fmtr " typechecker_trace { % a ; % a ; % a ; % a } " pp_kind t_kind pp_code_or_data code_or_data Size . pp_micheline_size micheline_size Size . pp consumed
|
let workload_to_sparse_vec ( trace : t ) = let ( name , { Size . traversal ; int_bytes ; string_bytes } , consumed ) = match trace with | Typechecker_workload { t_kind ; code_or_data ; micheline_size ; consumed } -> let name = Format . asprintf " % a_ % a " pp_kind t_kind pp_code_or_data code_or_data in ( name , micheline_size , consumed ) in let n s = name ^ " _ " ^ s in let vars = [ ( n " traversal " , Size . to_float traversal ) ; ( n " int_bytes " , Size . to_float int_bytes ) ; ( n " string_bytes " , Size . to_float string_bytes ) ; ( n " gas " , Size . to_float consumed ) ; ] in Sparse_vec . String . of_list vars
|
let data_typechecker_workload ctxt t_kind micheline_node ex_ty = let open Protocol in match ex_ty with | Script_ir_translator . Ex_ty ty -> let ctxt = Gas_helpers . set_limit ctxt in Lwt_main . run ( Script_ir_translator . parse_data ctxt ~ legacy : false ~ allow_forged : false ty micheline_node |> Lwt . map Environment . wrap_tzresult >>= fun res -> match res with | Ok ( _res , ctxt_after ) -> let micheline_size = Size . of_micheline micheline_node in let consumed = Alpha_context . Gas . consumed ~ since : ctxt ~ until : ctxt_after in let trace = Typechecker_workload { t_kind ; code_or_data = Data ; micheline_size ; consumed = Size . of_int ( Z . to_int ( Gas_helpers . fp_to_z consumed ) ) ; } in Lwt . return ( Some trace ) | Error errors -> Michelson_v1_error_reporter . report_errors ~ details : true ~ show_source : true Format . err_formatter errors ; Format . eprintf " . " @ ; Lwt . return None )
|
let code_typechecker_workload ( ctxt : Protocol . Alpha_context . context ) ( t_kind : kind ) ( code : Protocol . Alpha_context . Script . node ) ( bef : Protocol . Script_ir_translator . ex_stack_ty ) = let open Protocol in let ctxt = Gas_helpers . set_limit ctxt in let ( Script_ir_translator . Ex_stack_ty stack_ty ) = bef in Lwt_main . run ( Script_ir_translator . parse_instr Script_tc_context . data ctxt ~ legacy : false code stack_ty |> Lwt . map Environment . wrap_tzresult >>= fun res -> match res with | Ok ( _res , ctxt_after ) -> let micheline_size = Size . of_micheline code in let consumed = Alpha_context . Gas . consumed ~ since : ctxt ~ until : ctxt_after in let trace = Typechecker_workload { t_kind ; code_or_data = Code ; micheline_size ; consumed = Size . of_int ( Z . to_int ( Gas_helpers . fp_to_z consumed ) ) ; } in Lwt . return ( Some trace ) | Error errs -> Michelson_v1_error_reporter . report_errors ~ details : true ~ show_source : true Format . err_formatter errs ; Format . eprintf " . " @ ; Lwt . return None )
|
let is_inline_attribute = function | { txt ( " = inline " " | ocaml . inline " ) } -> true | _ -> false
|
let is_inlined_attribute = function | { txt ( " = inlined " " | ocaml . inlined " ) } -> true | { txt ( " = unrolled " " | ocaml . unrolled " ) } when Config . flambda -> true | _ -> false
|
let is_specialise_attribute = function | { txt ( " = specialise " " | ocaml . specialise " ) } when Config . flambda -> true | _ -> false
|
let is_specialised_attribute = function | { txt ( " = specialised " " | ocaml . specialised " ) } when Config . flambda -> true | _ -> false
|
let is_local_attribute = function | { txt ( " = local " " | ocaml . local " ) } -> true | _ -> false
|
let find_attribute p attributes = let inline_attribute , other_attributes = List . partition ( fun a -> p a . Parsetree . attr_name ) attributes in let attr = match inline_attribute with | [ ] -> None | [ attr ] -> Some attr | _ :: { Parsetree . attr_name = { txt ; loc } ; _ } :: _ -> Location . prerr_warning loc ( Warnings . Duplicated_attribute txt ) ; None in attr , other_attributes
|
let is_unrolled = function | { txt " = unrolled " " | ocaml . unrolled " } -> true | { txt " = inline " " | ocaml . inline " " | inlined " " | ocaml . inlined " } -> false | _ -> assert false
|
let get_id_payload = let open Parsetree in function | PStr [ ] -> Some " " | PStr [ { pstr_desc = Pstr_eval ( { pexp_desc } , [ ] ) } ] -> begin match pexp_desc with | Pexp_ident { txt = Longident . Lident id } -> Some id | _ -> None end | _ -> None
|
let parse_id_payload txt loc ~ default ~ empty cases payload = let [ @ local ] warn ( ) = let ( %> ) f g x = g ( f x ) in let msg = cases |> List . map ( fst %> Printf . sprintf " ' % s ' " ) |> String . concat " , " |> Printf . sprintf " It must be either % s or empty " in Location . prerr_warning loc ( Warnings . Attribute_payload ( txt , msg ) ) ; default in match get_id_payload payload with | Some " " -> empty | None -> warn ( ) | Some id -> match List . assoc_opt id cases with | Some r -> r | None -> warn ( )
|
let parse_inline_attribute attr = match attr with | None -> Default_inline | Some { Parsetree . attr_name = { txt ; loc } as id ; attr_payload = payload } -> let open Parsetree in if is_unrolled id then begin let warning txt = Warnings . Attribute_payload ( txt , " It must be an integer literal " ) in match payload with | PStr [ { pstr_desc = Pstr_eval ( { pexp_desc } , [ ] ) } ] -> begin match pexp_desc with | Pexp_constant ( Pconst_integer ( s , None ) ) -> begin try Unroll ( Misc . Int_literal_converter . int s ) with Failure _ -> Location . prerr_warning loc ( warning txt ) ; Default_inline end | _ -> Location . prerr_warning loc ( warning txt ) ; Default_inline end | _ -> Location . prerr_warning loc ( warning txt ) ; Default_inline end else parse_id_payload txt loc ~ default : Default_inline ~ empty : Always_inline [ " never " , Never_inline ; " always " , Always_inline ; ] payload
|
let parse_specialise_attribute attr = match attr with | None -> Default_specialise | Some { Parsetree . attr_name = { txt ; loc } ; attr_payload = payload } -> parse_id_payload txt loc ~ default : Default_specialise ~ empty : Always_specialise [ " never " , Never_specialise ; " always " , Always_specialise ; ] payload
|
let parse_local_attribute attr = match attr with | None -> Default_local | Some { Parsetree . attr_name = { txt ; loc } ; attr_payload = payload } -> parse_id_payload txt loc ~ default : Default_local ~ empty : Always_local [ " never " , Never_local ; " always " , Always_local ; " maybe " , Default_local ; ] payload
|
let get_inline_attribute l = let attr , _ = find_attribute is_inline_attribute l in parse_inline_attribute attr
|
let get_specialise_attribute l = let attr , _ = find_attribute is_specialise_attribute l in parse_specialise_attribute attr
|
let get_local_attribute l = let attr , _ = find_attribute is_local_attribute l in parse_local_attribute attr
|
let check_local_inline loc attr = match attr . local , attr . inline with | Always_local , ( Always_inline | Unroll _ ) -> Location . prerr_warning loc ( Warnings . Duplicated_attribute " local / inline " ) | _ -> ( )
|
let add_inline_attribute expr loc attributes = match expr , get_inline_attribute attributes with | expr , Default_inline -> expr | Lfunction ( { attr = { stub = false } as attr } as funct ) , inline -> begin match attr . inline with | Default_inline -> ( ) | Always_inline | Never_inline | Unroll _ -> Location . prerr_warning loc ( Warnings . Duplicated_attribute " inline " ) end ; let attr = { attr with inline } in check_local_inline loc attr ; Lfunction { funct with attr = attr } | expr , ( Always_inline | Never_inline | Unroll _ ) -> Location . prerr_warning loc ( Warnings . Misplaced_attribute " inline " ) ; expr
|
let add_specialise_attribute expr loc attributes = match expr , get_specialise_attribute attributes with | expr , Default_specialise -> expr | Lfunction ( { attr = { stub = false } as attr } as funct ) , specialise -> begin match attr . specialise with | Default_specialise -> ( ) | Always_specialise | Never_specialise -> Location . prerr_warning loc ( Warnings . Duplicated_attribute " specialise " ) end ; let attr = { attr with specialise } in Lfunction { funct with attr } | expr , ( Always_specialise | Never_specialise ) -> Location . prerr_warning loc ( Warnings . Misplaced_attribute " specialise " ) ; expr
|
let add_local_attribute expr loc attributes = match expr , get_local_attribute attributes with | expr , Default_local -> expr | Lfunction ( { attr = { stub = false } as attr } as funct ) , local -> begin match attr . local with | Default_local -> ( ) | Always_local | Never_local -> Location . prerr_warning loc ( Warnings . Duplicated_attribute " local " ) end ; let attr = { attr with local } in check_local_inline loc attr ; Lfunction { funct with attr } | expr , ( Always_local | Never_local ) -> Location . prerr_warning loc ( Warnings . Misplaced_attribute " local " ) ; expr
|
let get_and_remove_inlined_attribute e = let attr , exp_attributes = find_attribute is_inlined_attribute e . exp_attributes in let inlined = parse_inline_attribute attr in inlined , { e with exp_attributes }
|
let get_and_remove_inlined_attribute_on_module e = let rec get_and_remove mod_expr = let attr , mod_attributes = find_attribute is_inlined_attribute mod_expr . mod_attributes in let attr = parse_inline_attribute attr in let attr , mod_desc = match mod_expr . Typedtree . mod_desc with | Tmod_constraint ( me , mt , mtc , mc ) -> let inner_attr , me = get_and_remove me in let attr = match attr with | Always_inline | Never_inline | Unroll _ -> attr | Default_inline -> inner_attr in attr , Tmod_constraint ( me , mt , mtc , mc ) | md -> attr , md in attr , { mod_expr with mod_desc ; mod_attributes } in get_and_remove e
|
let get_and_remove_specialised_attribute e = let attr , exp_attributes = find_attribute is_specialised_attribute e . exp_attributes in let specialised = parse_specialise_attribute attr in specialised , { e with exp_attributes }
|
let get_tailcall_attribute e = let is_tailcall_attribute = function | { Parsetree . attr_name = { txt ( " = tailcall " " | ocaml . tailcall " ) } ; _ } -> true | _ -> false in let tailcalls , exp_attributes = List . partition is_tailcall_attribute e . exp_attributes in match tailcalls with | [ ] -> false , e | _ :: r -> begin match r with | [ ] -> ( ) | { Parsetree . attr_name = { txt ; loc } ; _ } :: _ -> Location . prerr_warning loc ( Warnings . Duplicated_attribute txt ) end ; true , { e with exp_attributes }
|
let check_attribute e { Parsetree . attr_name = { txt ; loc } ; _ } = match txt with | " inline " | " ocaml . inline " | " specialise " | " ocaml . specialise " -> begin match e . exp_desc with | Texp_function _ -> ( ) | _ -> Location . prerr_warning loc ( Warnings . Misplaced_attribute txt ) end | " inlined " | " ocaml . inlined " | " specialised " | " ocaml . specialised " | " tailcall " | " ocaml . tailcall " -> Location . prerr_warning loc ( Warnings . Misplaced_attribute txt ) | _ -> ( )
|
let check_attribute_on_module e { Parsetree . attr_name = { txt ; loc } ; _ } = match txt with | " inline " | " ocaml . inline " -> begin match e . mod_desc with | Tmod_functor _ -> ( ) | _ -> Location . prerr_warning loc ( Warnings . Misplaced_attribute txt ) end | " inlined " | " ocaml . inlined " -> Location . prerr_warning loc ( Warnings . Misplaced_attribute txt ) | _ -> ( )
|
let add_function_attributes lam loc attr = let lam = add_inline_attribute lam loc attr in let lam = add_specialise_attribute lam loc attr in let lam = add_local_attribute lam loc attr in lam
|
type error = Tags of label * label
|
let lfunction params body = if params = [ ] then body else match body with | Lfunction { kind = Curried ; params = params ' ; body = body ' ; attr ; loc } -> Lfunction { kind = Curried ; params = params @ params ' ; return = Pgenval ; body = body ' ; attr ; loc } | _ -> Lfunction { kind = Curried ; params ; return = Pgenval ; body ; attr = default_function_attribute ; loc = Location . none }
|
let lapply ap = match ap . ap_func with Lapply ap ' -> Lapply { ap with ap_func = ap ' . ap_func ; ap_args = ap ' . ap_args @ ap . ap_args } | _ -> Lapply ap
|
let mkappl ( func , args ) = Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = func ; ap_args = args ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } ; ;
|
let lsequence l1 l2 = if l2 = lambda_unit then l1 else Lsequence ( l1 , l2 )
|
let lfield v i = Lprim ( Pfield i , [ Lvar v ] , Location . none )
|
let transl_label l = share ( Const_immstring l )
|
let transl_meth_list lst = if lst = [ ] then Lconst ( Const_pointer 0 ) else share ( Const_block ( 0 , List . map ( fun lab -> Const_immstring lab ) lst ) )
|
let set_inst_var obj id expr = Lprim ( Psetfield_computed ( Typeopt . maybe_pointer expr , Assignment ) , [ Lvar obj ; Lvar id ; transl_exp expr ] , Location . none )
|
let transl_val tbl create name = mkappl ( oo_prim ( if create then " new_variable " else " get_variable " ) , [ Lvar tbl ; transl_label name ] )
|
let transl_vals tbl create strict vals rem = List . fold_right ( fun ( name , id ) rem -> Llet ( strict , Pgenval , id , transl_val tbl create name , rem ) ) vals rem
|
let meths_super tbl meths inh_meths = List . fold_right ( fun ( nm , id ) rem -> try ( nm , id , mkappl ( oo_prim " get_method " , [ Lvar tbl ; Lvar ( Meths . find nm meths ) ] ) ) :: rem with Not_found -> rem ) inh_meths [ ]
|
let bind_super tbl ( vals , meths ) cl_init = transl_vals tbl false StrictOpt vals ( List . fold_right ( fun ( _nm , id , def ) rem -> Llet ( StrictOpt , Pgenval , id , def , rem ) ) meths cl_init )
|
let create_object cl obj init = let obj ' = Ident . create_local " self " in let ( inh_init , obj_init , has_init ) = init obj ' in if obj_init = lambda_unit then ( inh_init , mkappl ( oo_prim ( if has_init then " create_object_and_run_initializers " else " create_object_opt " ) , [ obj ; Lvar cl ] ) ) else begin ( inh_init , Llet ( Strict , Pgenval , obj ' , mkappl ( oo_prim " create_object_opt " , [ obj ; Lvar cl ] ) , Lsequence ( obj_init , if not has_init then Lvar obj ' else mkappl ( oo_prim " run_initializers_opt " , [ obj ; Lvar obj ' ; Lvar cl ] ) ) ) ) end
|
let name_pattern default p = match p . pat_desc with | Tpat_var ( id , _ ) -> id | Tpat_alias ( _ , id , _ ) -> id | _ -> Ident . create_local default
|
let rec build_object_init cl_table obj params inh_init obj_init cl = match cl . cl_desc with Tcl_ident ( path , _ , _ ) -> let obj_init = Ident . create_local " obj_init " in let envs , inh_init = inh_init in let env = match envs with None -> [ ] | Some envs -> [ Lprim ( Pfield ( List . length inh_init + 1 ) , [ Lvar envs ] , Location . none ) ] in let path_lam = transl_class_path cl . cl_loc cl . cl_env path in ( ( envs , ( path , path_lam , obj_init ) :: inh_init ) , mkappl ( Lvar obj_init , env @ [ obj ] ) ) | Tcl_structure str -> create_object cl_table obj ( fun obj -> let ( inh_init , obj_init , has_init ) = List . fold_right ( fun field ( inh_init , obj_init , has_init ) -> match field . cf_desc with Tcf_inherit ( _ , cl , _ , _ , _ ) -> let ( inh_init , obj_init ' ) = build_object_init cl_table ( Lvar obj ) [ ] inh_init ( fun _ -> lambda_unit ) cl in ( inh_init , lsequence obj_init ' obj_init , true ) | Tcf_val ( _ , _ , id , Tcfk_concrete ( _ , exp ) , _ ) -> ( inh_init , lsequence ( set_inst_var obj id exp ) obj_init , has_init ) | Tcf_method _ | Tcf_val _ | Tcf_constraint _ | Tcf_attribute _ -> ( inh_init , obj_init , has_init ) | Tcf_initializer _ -> ( inh_init , obj_init , true ) ) str . cstr_fields ( inh_init , obj_init obj , false ) in ( inh_init , List . fold_right ( fun ( id , expr ) rem -> lsequence ( Lifused ( id , set_inst_var obj id expr ) ) rem ) params obj_init , has_init ) ) | Tcl_fun ( _ , pat , vals , cl , partial ) -> let ( inh_init , obj_init ) = build_object_init cl_table obj ( vals @ params ) inh_init obj_init cl in ( inh_init , let build params rem = let param = name_pattern " param " pat in Lfunction { kind = Curried ; params = ( param , Pgenval ) :: params ; return = Pgenval ; attr = default_function_attribute ; loc = pat . pat_loc ; body = Matching . for_function pat . pat_loc None ( Lvar param ) [ pat , rem ] partial } in begin match obj_init with Lfunction { kind = Curried ; params ; body = rem } -> build params rem | rem -> build [ ] rem end ) | Tcl_apply ( cl , oexprs ) -> let ( inh_init , obj_init ) = build_object_init cl_table obj params inh_init obj_init cl in ( inh_init , transl_apply obj_init oexprs Location . none ) | Tcl_let ( rec_flag , defs , vals , cl ) -> let ( inh_init , obj_init ) = build_object_init cl_table obj ( vals @ params ) inh_init obj_init cl in ( inh_init , Translcore . transl_let rec_flag defs obj_init ) | Tcl_open ( _ , cl ) | Tcl_constraint ( cl , _ , _ , _ , _ ) -> build_object_init cl_table obj params inh_init obj_init cl
|
let rec build_object_init_0 cl_table params cl copy_env subst_env top ids = match cl . cl_desc with Tcl_let ( _rec_flag , _defs , vals , cl ) -> build_object_init_0 cl_table ( vals @ params ) cl copy_env subst_env top ids | _ -> let self = Ident . create_local " self " in let env = Ident . create_local " env " in let obj = if ids = [ ] then lambda_unit else Lvar self in let envs = if top then None else Some env in let ( ( _ , inh_init ) , obj_init ) = build_object_init cl_table obj params ( envs , [ ] ) copy_env cl in let obj_init = if ids = [ ] then obj_init else lfunction [ self , Pgenval ] obj_init in ( inh_init , lfunction [ env , Pgenval ] ( subst_env env inh_init obj_init ) )
|
let bind_method tbl lab id cl_init = Llet ( Strict , Pgenval , id , mkappl ( oo_prim " get_method_label " , [ Lvar tbl ; transl_label lab ] ) , cl_init )
|
let bind_methods tbl meths vals cl_init = let methl = Meths . fold ( fun lab id tl -> ( lab , id ) :: tl ) meths [ ] in let len = List . length methl and nvals = List . length vals in if len < 2 && nvals = 0 then Meths . fold ( bind_method tbl ) meths cl_init else if len = 0 && nvals < 2 then transl_vals tbl true Strict vals cl_init else let ids = Ident . create_local " ids " in let i = ref ( len + nvals ) in let getter , names = if nvals = 0 then " get_method_labels " , [ ] else " new_methods_variables " , [ transl_meth_list ( List . map fst vals ) ] in Llet ( Strict , Pgenval , ids , mkappl ( oo_prim getter , [ Lvar tbl ; transl_meth_list ( List . map fst methl ) ] @ names ) , List . fold_right ( fun ( _lab , id ) lam -> decr i ; Llet ( StrictOpt , Pgenval , id , lfield ids ! i , lam ) ) ( methl @ vals ) cl_init )
|
let output_methods tbl methods lam = match methods with [ ] -> lam | [ lab ; code ] -> lsequence ( mkappl ( oo_prim " set_method " , [ Lvar tbl ; lab ; code ] ) ) lam | _ -> lsequence ( mkappl ( oo_prim " set_methods " , [ Lvar tbl ; Lprim ( Pmakeblock ( 0 , Immutable , None ) , methods , Location . none ) ] ) ) lam
|
let rec ignore_cstrs cl = match cl . cl_desc with Tcl_constraint ( cl , _ , _ , _ , _ ) -> ignore_cstrs cl | Tcl_apply ( cl , _ ) -> ignore_cstrs cl | _ -> cl
|
let rec index a = function [ ] -> raise Not_found | b :: l -> if b = a then 0 else 1 + index a l
|
let bind_id_as_val ( id , _ ) = ( " " , id )
|
let rec build_class_init cla cstr super inh_init cl_init msubst top cl = match cl . cl_desc with | Tcl_ident _ -> begin match inh_init with | ( _ , path_lam , obj_init ) :: inh_init -> ( inh_init , Llet ( Strict , Pgenval , obj_init , mkappl ( Lprim ( Pfield 1 , [ path_lam ] , Location . none ) , Lvar cla :: if top then [ Lprim ( Pfield 3 , [ path_lam ] , Location . none ) ] else [ ] ) , bind_super cla super cl_init ) ) | _ -> assert false end | Tcl_structure str -> let cl_init = bind_super cla super cl_init in let ( inh_init , cl_init , methods , values ) = List . fold_right ( fun field ( inh_init , cl_init , methods , values ) -> match field . cf_desc with Tcf_inherit ( _ , cl , _ , vals , meths ) -> let cl_init = output_methods cla methods cl_init in let inh_init , cl_init = build_class_init cla false ( vals , meths_super cla str . cstr_meths meths ) inh_init cl_init msubst top cl in ( inh_init , cl_init , [ ] , values ) | Tcf_val ( name , _ , id , _ , over ) -> let values = if over then values else ( name . txt , id ) :: values in ( inh_init , cl_init , methods , values ) | Tcf_method ( _ , _ , Tcfk_virtual _ ) | Tcf_constraint _ -> ( inh_init , cl_init , methods , values ) | Tcf_method ( name , _ , Tcfk_concrete ( _ , exp ) ) -> let met_code = msubst true ( transl_exp exp ) in let met_code = if ! Clflags . native_code && List . length met_code = 1 then let met = Ident . create_local ( " method_ " ^ name . txt ) in [ Llet ( Strict , Pgenval , met , List . hd met_code , Lvar met ) ] else met_code in ( inh_init , cl_init , Lvar ( Meths . find name . txt str . cstr_meths ) :: met_code @ methods , values ) | Tcf_initializer exp -> ( inh_init , Lsequence ( mkappl ( oo_prim " add_initializer " , Lvar cla :: msubst false ( transl_exp exp ) ) , cl_init ) , methods , values ) | Tcf_attribute _ -> ( inh_init , cl_init , methods , values ) ) str . cstr_fields ( inh_init , cl_init , [ ] , [ ] ) in let cl_init = output_methods cla methods cl_init in ( inh_init , bind_methods cla str . cstr_meths values cl_init ) | Tcl_fun ( _ , _pat , vals , cl , _ ) -> let ( inh_init , cl_init ) = build_class_init cla cstr super inh_init cl_init msubst top cl in let vals = List . map bind_id_as_val vals in ( inh_init , transl_vals cla true StrictOpt vals cl_init ) | Tcl_apply ( cl , _exprs ) -> build_class_init cla cstr super inh_init cl_init msubst top cl | Tcl_let ( _rec_flag , _defs , vals , cl ) -> let ( inh_init , cl_init ) = build_class_init cla cstr super inh_init cl_init msubst top cl in let vals = List . map bind_id_as_val vals in ( inh_init , transl_vals cla true StrictOpt vals cl_init ) | Tcl_constraint ( cl , _ , vals , meths , concr_meths ) -> let virt_meths = List . filter ( fun lab -> not ( Concr . mem lab concr_meths ) ) meths in let concr_meths = Concr . elements concr_meths in let narrow_args = [ Lvar cla ; transl_meth_list vals ; transl_meth_list virt_meths ; transl_meth_list concr_meths ] in let cl = ignore_cstrs cl in begin match cl . cl_desc , inh_init with | Tcl_ident ( path , _ , _ ) , ( path ' , path_lam , obj_init ) :: inh_init -> assert ( Path . same path path ' ) ; let inh = Ident . create_local " inh " and ofs = List . length vals + 1 and valids , methids = super in let cl_init = List . fold_left ( fun init ( nm , id , _ ) -> Llet ( StrictOpt , Pgenval , id , lfield inh ( index nm concr_meths + ofs ) , init ) ) cl_init methids in let cl_init = List . fold_left ( fun init ( nm , id ) -> Llet ( StrictOpt , Pgenval , id , lfield inh ( index nm vals + 1 ) , init ) ) cl_init valids in ( inh_init , Llet ( Strict , Pgenval , inh , mkappl ( oo_prim " inherits " , narrow_args @ [ path_lam ; Lconst ( Const_pointer ( if top then 1 else 0 ) ) ] ) , Llet ( StrictOpt , Pgenval , obj_init , lfield inh 0 , cl_init ) ) ) | _ -> let core cl_init = build_class_init cla true super inh_init cl_init msubst top cl in if cstr then core cl_init else let ( inh_init , cl_init ) = core ( Lsequence ( mkappl ( oo_prim " widen " , [ Lvar cla ] ) , cl_init ) ) in ( inh_init , Lsequence ( mkappl ( oo_prim " narrow " , narrow_args ) , cl_init ) ) end | Tcl_open ( _ , cl ) -> build_class_init cla cstr super inh_init cl_init msubst top cl
|
let rec build_class_lets cl = match cl . cl_desc with Tcl_let ( rec_flag , defs , _vals , cl ' ) -> let env , wrap = build_class_lets cl ' in ( env , fun x -> Translcore . transl_let rec_flag defs ( wrap x ) ) | _ -> ( cl . cl_env , fun x -> x )
|
let rec get_class_meths cl = match cl . cl_desc with Tcl_structure cl -> Meths . fold ( fun _ -> Ident . Set . add ) cl . cstr_meths Ident . Set . empty | Tcl_ident _ -> Ident . Set . empty | Tcl_fun ( _ , _ , _ , cl , _ ) | Tcl_let ( _ , _ , _ , cl ) | Tcl_apply ( cl , _ ) | Tcl_open ( _ , cl ) | Tcl_constraint ( cl , _ , _ , _ , _ ) -> get_class_meths cl
|
let rec transl_class_rebind obj_init cl vf = match cl . cl_desc with Tcl_ident ( path , _ , _ ) -> if vf = Concrete then begin try if ( Env . find_class path cl . cl_env ) . cty_new = None then raise Exit with Not_found -> raise Exit end ; let path_lam = transl_class_path cl . cl_loc cl . cl_env path in ( path , path_lam , obj_init ) | Tcl_fun ( _ , pat , _ , cl , partial ) -> let path , path_lam , obj_init = transl_class_rebind obj_init cl vf in let build params rem = let param = name_pattern " param " pat in Lfunction { kind = Curried ; params = ( param , Pgenval ) :: params ; return = Pgenval ; attr = default_function_attribute ; loc = pat . pat_loc ; body = Matching . for_function pat . pat_loc None ( Lvar param ) [ pat , rem ] partial } in ( path , path_lam , match obj_init with Lfunction { kind = Curried ; params ; body } -> build params body | rem -> build [ ] rem ) | Tcl_apply ( cl , oexprs ) -> let path , path_lam , obj_init = transl_class_rebind obj_init cl vf in ( path , path_lam , transl_apply obj_init oexprs Location . none ) | Tcl_let ( rec_flag , defs , _vals , cl ) -> let path , path_lam , obj_init = transl_class_rebind obj_init cl vf in ( path , path_lam , Translcore . transl_let rec_flag defs obj_init ) | Tcl_structure _ -> raise Exit | Tcl_constraint ( cl ' , _ , _ , _ , _ ) -> let path , path_lam , obj_init = transl_class_rebind obj_init cl ' vf in let rec check_constraint = function Cty_constr ( path ' , _ , _ ) when Path . same path path ' -> ( ) | Cty_arrow ( _ , _ , cty ) -> check_constraint cty | _ -> raise Exit in check_constraint cl . cl_type ; ( path , path_lam , obj_init ) | Tcl_open ( _ , cl ) -> transl_class_rebind obj_init cl vf
|
let rec transl_class_rebind_0 ( self : Ident . t ) obj_init cl vf = match cl . cl_desc with Tcl_let ( rec_flag , defs , _vals , cl ) -> let path , path_lam , obj_init = transl_class_rebind_0 self obj_init cl vf in ( path , path_lam , Translcore . transl_let rec_flag defs obj_init ) | _ -> let path , path_lam , obj_init = transl_class_rebind obj_init cl vf in ( path , path_lam , lfunction [ self , Pgenval ] obj_init )
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.