text
stringlengths
0
601k
let transl_type_extension env rootpath tyext body = List . fold_right ( fun ext body -> let lam = transl_extension_constructor env ( field_path rootpath ext . ext_id ) ext in Llet ( Strict , Pgenval , ext . ext_id , lam , body ) ) tyext . tyext_constructors body
let rec apply_coercion loc strict restr arg = match restr with Tcoerce_none -> arg | Tcoerce_structure ( pos_cc_list , id_pos_list ) -> name_lambda strict arg ( fun id -> let get_field pos = if pos < 0 then lambda_unit else Lprim ( Pfield pos , [ Lvar id ] , loc ) in let lam = Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( apply_coercion_field loc get_field ) pos_cc_list , loc ) in wrap_id_pos_list loc id_pos_list get_field lam ) | Tcoerce_functor ( cc_arg , cc_res ) -> let param = Ident . create_local " funarg " in let carg = apply_coercion loc Alias cc_arg ( Lvar param ) in apply_coercion_result loc strict arg [ param , Pgenval ] [ carg ] cc_res | Tcoerce_primitive { pc_loc ; pc_desc ; pc_env ; pc_type ; } -> Translprim . transl_primitive pc_loc pc_desc pc_env pc_type None | Tcoerce_alias ( env , path , cc ) -> let lam = transl_module_path loc env path in name_lambda strict arg ( fun _ -> apply_coercion loc Alias cc lam ) apply_coercion loc Alias cc ( get_field pos ) match cc_res with | Tcoerce_functor ( cc_arg , cc_res ) -> let param = Ident . create_local " funarg " in let arg = apply_coercion loc Alias cc_arg ( Lvar param ) in apply_coercion_result loc strict funct ( ( param , Pgenval ) :: params ) ( arg :: args ) cc_res | _ -> name_lambda strict funct ( fun id -> Lfunction { kind = Curried ; params = List . rev params ; return = Pgenval ; attr = { default_function_attribute with is_a_functor = true ; stub = true ; } ; loc = loc ; body = apply_coercion loc Strict cc_res ( Lapply { ap_should_be_tailcall = false ; ap_loc = loc ; ap_func = Lvar id ; ap_args = List . rev args ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } ) } ) let fv = free_variables lam in let ( lam , s ) = List . fold_left ( fun ( lam , s ) ( id ' , pos , c ) -> if Ident . Set . mem id ' fv then let id ' ' = Ident . create_local ( Ident . name id ' ) in ( Llet ( Alias , Pgenval , id ' ' , apply_coercion loc Alias c ( get_field pos ) , lam ) , Ident . Map . add id ' id ' ' s ) else ( lam , s ) ) ( lam , Ident . Map . empty ) id_pos_list in if s == Ident . Map . empty then lam else Lambda . rename s lam
let rec compose_coercions c1 c2 = match ( c1 , c2 ) with ( Tcoerce_none , c2 ) -> c2 | ( c1 , Tcoerce_none ) -> c1 | ( Tcoerce_structure ( pc1 , ids1 ) , Tcoerce_structure ( pc2 , ids2 ) ) -> let v2 = Array . of_list pc2 in let ids1 = List . map ( fun ( id , pos1 , c1 ) -> let ( pos2 , c2 ) = v2 . ( pos1 ) in ( id , pos2 , compose_coercions c1 c2 ) ) ids1 in Tcoerce_structure ( List . map ( fun pc -> match pc with | _ , ( Tcoerce_primitive _ | Tcoerce_alias _ ) -> pc | ( p1 , c1 ) -> let ( p2 , c2 ) = v2 . ( p1 ) in ( p2 , compose_coercions c1 c2 ) ) pc1 , ids1 @ ids2 ) | ( Tcoerce_functor ( arg1 , res1 ) , Tcoerce_functor ( arg2 , res2 ) ) -> Tcoerce_functor ( compose_coercions arg2 arg1 , compose_coercions res1 res2 ) | ( c1 , Tcoerce_alias ( env , path , c2 ) ) -> Tcoerce_alias ( env , path , compose_coercions c1 c2 ) | ( _ , _ ) -> fatal_error " Translmod . compose_coercions "
let primitive_declarations = ref ( [ ] : Primitive . description list )
let record_primitive = function | { val_kind = Val_prim p ; val_loc } -> Translprim . check_primitive_arity val_loc p ; primitive_declarations := p :: ! primitive_declarations | _ -> ( )
let mod_prim = Lambda . transl_prim " CamlinternalMod "
let undefined_location loc = let ( fname , line , char ) = Location . get_pos_info loc . Location . loc_start in Lconst ( Const_block ( 0 , [ Const_base ( Const_string ( fname , None ) ) ; Const_base ( Const_int line ) ; Const_base ( Const_int char ) ] ) )
let init_shape id modl = let rec init_shape_mod subid loc env mty = match Mtype . scrape env mty with Mty_ident _ | Mty_alias _ -> raise ( Initialization_failure ( Unsafe { reason = Unsafe_module_binding ; loc ; subid } ) ) | Mty_signature sg -> Const_block ( 0 , [ Const_block ( 0 , init_shape_struct env sg ) ] ) | Mty_functor _ -> raise ( Initialization_failure ( Unsafe { reason = Unsafe_functor ; loc ; subid } ) ) and init_shape_struct env sg = match sg with [ ] -> [ ] | Sig_value ( subid , { val_kind = Val_reg ; val_type = ty ; val_loc = loc } , _ ) :: rem -> let init_v = match Ctype . expand_head env ty with { desc = Tarrow ( _ , _ , _ , _ ) } -> Const_pointer 0 | { desc = Tconstr ( p , _ , _ ) } when Path . same p Predef . path_lazy_t -> Const_pointer 1 | _ -> let not_a_function = Unsafe { reason = Unsafe_non_function ; loc ; subid } in raise ( Initialization_failure not_a_function ) in init_v :: init_shape_struct env rem | Sig_value ( _ , { val_kind = Val_prim _ } , _ ) :: rem -> init_shape_struct env rem | Sig_value _ :: _rem -> assert false | Sig_type ( id , tdecl , _ , _ ) :: rem -> init_shape_struct ( Env . add_type ~ check : false id tdecl env ) rem | Sig_typext ( subid , { ext_loc = loc } , _ , _ ) :: _ -> raise ( Initialization_failure ( Unsafe { reason = Unsafe_typext ; loc ; subid } ) ) | Sig_module ( id , Mp_present , md , _ , _ ) :: rem -> init_shape_mod id md . md_loc env md . md_type :: init_shape_struct ( Env . add_module_declaration ~ check : false id Mp_present md env ) rem | Sig_module ( id , Mp_absent , md , _ , _ ) :: rem -> init_shape_struct ( Env . add_module_declaration ~ check : false id Mp_absent md env ) rem | Sig_modtype ( id , minfo , _ ) :: rem -> init_shape_struct ( Env . add_modtype id minfo env ) rem | Sig_class _ :: rem -> Const_pointer 2 :: init_shape_struct env rem | Sig_class_type _ :: rem -> init_shape_struct env rem in try Ok ( undefined_location modl . mod_loc , Lconst ( init_shape_mod id modl . mod_loc modl . mod_env modl . mod_type ) ) with Initialization_failure reason -> Result . Error ( reason )
type binding_status = | Undefined | Inprogress of int option | Defined
type id_or_ignore_loc = | Id of Ident . t | Ignore_loc of Location . t
let extract_unsafe_cycle id status init cycle_start = let info i = match init . ( i ) with | Result . Error r -> begin match id . ( i ) with | Id id -> id , r | Ignore_loc _ -> assert false end | Ok _ -> assert false in let rec collect stop l i = match status . ( i ) with | Inprogress None | Undefined | Defined -> assert false | Inprogress Some i when i = stop -> info i :: l | Inprogress Some i -> collect stop ( info i :: l ) i in collect cycle_start [ ] cycle_start
let reorder_rec_bindings bindings = let id = Array . of_list ( List . map ( fun ( id , _ , _ , _ ) -> id ) bindings ) and loc = Array . of_list ( List . map ( fun ( _ , loc , _ , _ ) -> loc ) bindings ) and init = Array . of_list ( List . map ( fun ( _ , _ , init , _ ) -> init ) bindings ) and rhs = Array . of_list ( List . map ( fun ( _ , _ , _ , rhs ) -> rhs ) bindings ) in let fv = Array . map Lambda . free_variables rhs in let num_bindings = Array . length id in let status = Array . make num_bindings Undefined in let res = ref [ ] in let is_unsafe i = match init . ( i ) with | Ok _ -> false | Result . Error _ -> true in let init_res i = match init . ( i ) with | Result . Error _ -> None | Ok ( a , b ) -> Some ( a , b ) in let rec emit_binding parent i = match status . ( i ) with Defined -> ( ) | Inprogress _ -> status . ( i ) <- Inprogress parent ; let cycle = extract_unsafe_cycle id status init i in raise ( Error ( loc . ( i ) , Circular_dependency cycle ) ) | Undefined -> if is_unsafe i then begin status . ( i ) <- Inprogress parent ; for j = 0 to num_bindings - 1 do match id . ( j ) with | Id id when Ident . Set . mem id fv . ( i ) -> emit_binding ( Some i ) j | _ -> ( ) done end ; res := ( id . ( i ) , init_res i , rhs . ( i ) ) :: ! res ; status . ( i ) <- Defined in for i = 0 to num_bindings - 1 do match status . ( i ) with Undefined -> emit_binding None i | Inprogress _ -> assert false | Defined -> ( ) done ; List . rev ! res
let eval_rec_bindings bindings cont = let rec bind_inits = function [ ] -> bind_strict bindings | ( Ignore_loc _ , _ , _ ) :: rem | ( _ , None , _ ) :: rem -> bind_inits rem | ( Id id , Some ( loc , shape ) , _rhs ) :: rem -> Llet ( Strict , Pgenval , id , Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = mod_prim " init_mod " ; ap_args [ = loc ; shape ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } , bind_inits rem ) and bind_strict = function [ ] -> patch_forwards bindings | ( Ignore_loc loc , None , rhs ) :: rem -> Lsequence ( Lprim ( Pignore , [ rhs ] , loc ) , bind_strict rem ) | ( Id id , None , rhs ) :: rem -> Llet ( Strict , Pgenval , id , rhs , bind_strict rem ) | ( _id , Some _ , _rhs ) :: rem -> bind_strict rem and patch_forwards = function [ ] -> cont | ( Ignore_loc _ , _ , _rhs ) :: rem | ( _ , None , _rhs ) :: rem -> patch_forwards rem | ( Id id , Some ( _loc , shape ) , rhs ) :: rem -> Lsequence ( Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = mod_prim " update_mod " ; ap_args [ = shape ; Lvar id ; rhs ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } , patch_forwards rem ) in bind_inits bindings
let compile_recmodule compile_rhs bindings cont = eval_rec_bindings ( reorder_rec_bindings ( List . map ( fun { mb_id = id ; mb_name ; mb_expr = modl ; mb_loc = loc ; _ } -> let id_or_ignore_loc , shape = match id with | None -> Ignore_loc mb_name . loc , Result . Error Unnamed | Some id -> Id id , init_shape id modl in ( id_or_ignore_loc , modl . mod_loc , shape , compile_rhs id modl loc ) ) bindings ) ) cont
let transl_class_bindings cl_list = let ids = List . map ( fun ( ci , _ ) -> ci . ci_id_class ) cl_list in ( ids , List . map ( fun ( { ci_id_class = id ; ci_expr = cl ; ci_virt = vf } , meths ) -> ( id , transl_class ids id meths cl vf ) ) cl_list )
let merge_inline_attributes attr1 attr2 loc = match Lambda . merge_inline_attributes attr1 attr2 with | Some attr -> attr | None -> raise ( Error ( loc , Conflicting_inline_attributes ) )
let merge_functors mexp coercion root_path = let rec merge mexp coercion path acc inline_attribute = let finished = acc , mexp , path , coercion , inline_attribute in match mexp . mod_desc with | Tmod_functor ( param , body ) -> let inline_attribute ' = Translattribute . get_inline_attribute mexp . mod_attributes in let arg_coercion , res_coercion = match coercion with | Tcoerce_none -> Tcoerce_none , Tcoerce_none | Tcoerce_functor ( arg_coercion , res_coercion ) -> arg_coercion , res_coercion | _ -> fatal_error " Translmod . merge_functors : bad coercion " in let loc = mexp . mod_loc in let path , param = match param with | Unit -> None , Ident . create_local " " * | Named ( None , _ , _ ) -> let id = Ident . create_local " _ " in functor_path path id , id | Named ( Some id , _ , _ ) -> functor_path path id , id in let inline_attribute = merge_inline_attributes inline_attribute inline_attribute ' loc in merge body res_coercion path ( ( param , loc , arg_coercion ) :: acc ) inline_attribute | _ -> finished in merge mexp coercion root_path [ ] Default_inline
let rec compile_functor mexp coercion root_path loc = let functor_params_rev , body , body_path , res_coercion , inline_attribute = merge_functors mexp coercion root_path in assert ( List . length functor_params_rev >= 1 ) ; let params , body = List . fold_left ( fun ( params , body ) ( param , loc , arg_coercion ) -> let param ' = Ident . rename param in let arg = apply_coercion loc Alias arg_coercion ( Lvar param ' ) in let params = ( param ' , Pgenval ) :: params in let body = Llet ( Alias , Pgenval , param , arg , body ) in params , body ) ( [ ] , transl_module res_coercion body_path body ) functor_params_rev in Lfunction { kind = Curried ; params ; return = Pgenval ; attr = { inline = inline_attribute ; specialise = Default_specialise ; local = Default_local ; is_a_functor = true ; stub = false ; } ; loc ; body ; } List . iter ( Translattribute . check_attribute_on_module mexp ) mexp . mod_attributes ; let loc = mexp . mod_loc in match mexp . mod_desc with | Tmod_ident ( path , _ ) -> apply_coercion loc Strict cc ( transl_module_path loc mexp . mod_env path ) | Tmod_structure str -> fst ( transl_struct loc [ ] cc rootpath str ) | Tmod_functor _ -> oo_wrap mexp . mod_env true ( fun ( ) -> compile_functor mexp cc rootpath loc ) ( ) | Tmod_apply ( funct , arg , ccarg ) -> let inlined_attribute , funct = Translattribute . get_and_remove_inlined_attribute_on_module funct in oo_wrap mexp . mod_env true ( apply_coercion loc Strict cc ) ( Lapply { ap_should_be_tailcall = false ; ap_loc = loc ; ap_func = transl_module Tcoerce_none None funct ; ap_args [ = transl_module ccarg None arg ] ; ap_inlined = inlined_attribute ; ap_specialised = Default_specialise } ) | Tmod_constraint ( arg , _ , _ , ccarg ) -> transl_module ( compose_coercions cc ccarg ) rootpath arg | Tmod_unpack ( arg , _ ) -> apply_coercion loc Strict cc ( Translcore . transl_exp arg ) transl_structure loc fields cc rootpath str . str_final_env str . str_items [ ] -> let body , size = match cc with Tcoerce_none -> Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun id -> Lvar id ) ( List . rev fields ) , loc ) , List . length fields | Tcoerce_structure ( pos_cc_list , id_pos_list ) -> let v = Array . of_list ( List . rev fields ) in let get_field pos = if pos < 0 then lambda_unit else Lvar v . ( pos ) in let ids = List . fold_right Ident . Set . add fields Ident . Set . empty in let lam = Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun ( pos , cc ) -> match cc with Tcoerce_primitive p -> Translprim . transl_primitive p . pc_loc p . pc_desc p . pc_env p . pc_type None | _ -> apply_coercion loc Strict cc ( get_field pos ) ) pos_cc_list , loc ) and id_pos_list = List . filter ( fun ( id , _ , _ ) -> not ( Ident . Set . mem id ids ) ) id_pos_list in wrap_id_pos_list loc id_pos_list get_field lam , List . length pos_cc_list | _ -> fatal_error " Translmod . transl_structure " in ( if ! Clflags . debug && not ! Clflags . native_code then Levent ( body , { lev_loc = loc ; lev_kind = Lev_pseudo ; lev_repr = None ; lev_env = final_env } ) else body ) , size | item :: rem -> match item . str_desc with | Tstr_eval ( expr , _ ) -> let body , size = transl_structure loc fields cc rootpath final_env rem in Lsequence ( transl_exp expr , body ) , size | Tstr_value ( rec_flag , pat_expr_list ) -> let mk_lam_let = transl_let rec_flag pat_expr_list in let ext_fields = List . rev_append ( let_bound_idents pat_expr_list ) fields in let body , size = transl_structure loc ext_fields cc rootpath final_env rem in mk_lam_let body , size | Tstr_primitive descr -> record_primitive descr . val_val ; transl_structure loc fields cc rootpath final_env rem | Tstr_type _ -> transl_structure loc fields cc rootpath final_env rem | Tstr_typext ( tyext ) -> let ids = List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors in let body , size = transl_structure loc ( List . rev_append ids fields ) cc rootpath final_env rem in transl_type_extension item . str_env rootpath tyext body , size | Tstr_exception ext -> let id = ext . tyexn_constructor . ext_id in let path = field_path rootpath id in let body , size = transl_structure loc ( id :: fields ) cc rootpath final_env rem in Llet ( Strict , Pgenval , id , transl_extension_constructor item . str_env path ext . tyexn_constructor , body ) , size | Tstr_module ( { mb_presence = Mp_present } as mb ) -> let id = mb . mb_id in let module_body = transl_module Tcoerce_none ( Option . bind id ( field_path rootpath ) ) mb . mb_expr in let module_body = Translattribute . add_inline_attribute module_body mb . mb_loc mb . mb_attributes in let body , size = transl_structure loc ( cons_opt id fields ) cc rootpath final_env rem in begin match id with | None -> Lsequence ( Lprim ( Pignore , [ module_body ] , mb . mb_name . loc ) , body ) , size | Some id -> let module_body = Levent ( module_body , { lev_loc = mb . mb_loc ; lev_kind = Lev_module_definition id ; lev_repr = None ; lev_env = Env . empty ; } ) in Llet ( pure_module mb . mb_expr , Pgenval , id , module_body , body ) , size end | Tstr_module { mb_presence = Mp_absent } -> transl_structure loc fields cc rootpath final_env rem | Tstr_recmodule bindings -> let ext_fields = List . rev_append ( List . filter_map ( fun mb -> mb . mb_id ) bindings ) fields in let body , size = transl_structure loc ext_fields cc rootpath final_env rem in let lam = compile_recmodule ( fun id modl loc -> match id with | None -> transl_module Tcoerce_none None modl | Some id -> let module_body = transl_module Tcoerce_none ( field_path rootpath id ) modl in Levent ( module_body , { lev_loc = loc ; lev_kind = Lev_module_definition id ; lev_repr = None ; lev_env = Env . empty ; } ) ) bindings body in lam , size | Tstr_class cl_list -> let ( ids , class_bindings ) = transl_class_bindings cl_list in let body , size = transl_structure loc ( List . rev_append ids fields ) cc rootpath final_env rem in Lletrec ( class_bindings , body ) , size | Tstr_include incl -> let ids = bound_value_identifiers incl . incl_type in let modl = incl . incl_mod in let mid = Ident . create_local " include " in let rec rebind_idents pos newfields = function [ ] -> transl_structure loc newfields cc rootpath final_env rem | id :: ids -> let body , size = rebind_idents ( pos + 1 ) ( id :: newfields ) ids in Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , incl . incl_loc ) , body ) , size in let body , size = rebind_idents 0 fields ids in Llet ( pure_module modl , Pgenval , mid , transl_module Tcoerce_none None modl , body ) , size | Tstr_open od -> let pure = pure_module od . open_expr in begin match od . open_bound_items with | [ ] when pure = Alias -> transl_structure loc fields cc rootpath final_env rem | _ -> let ids = bound_value_identifiers od . open_bound_items in let mid = Ident . create_local " open " in let rec rebind_idents pos newfields = function [ ] -> transl_structure loc newfields cc rootpath final_env rem | id :: ids -> let body , size = rebind_idents ( pos + 1 ) ( id :: newfields ) ids in Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , od . open_loc ) , body ) , size in let body , size = rebind_idents 0 fields ids in Llet ( pure , Pgenval , mid , transl_module Tcoerce_none None od . open_expr , body ) , size end | Tstr_modtype _ | Tstr_class_type _ | Tstr_attribute _ -> transl_structure loc fields cc rootpath final_env rem
let _ = Translcore . transl_module := transl_module
let scan_used_globals lam = let globals = ref Ident . Set . empty in let rec scan lam = Lambda . iter_head_constructor scan lam ; match lam with Lprim ( ( Pgetglobal id | Psetglobal id ) , _ , _ ) -> globals := Ident . Set . add id ! globals | _ -> ( ) in scan lam ; ! globals
let required_globals ~ flambda body = let globals = scan_used_globals body in let add_global id req = if not flambda && Ident . Set . mem id globals then req else Ident . Set . add id req in let required = List . fold_left ( fun acc path -> add_global ( Path . head path ) acc ) ( if flambda then globals else Ident . Set . empty ) ( Translprim . get_used_primitives ( ) ) in let required = List . fold_right add_global ( Env . get_required_globals ( ) ) required in Env . reset_required_globals ( ) ; Translprim . clear_used_primitives ( ) ; required
let transl_implementation_flambda module_name ( str , cc ) = reset_labels ( ) ; primitive_declarations := [ ] ; Translprim . clear_used_primitives ( ) ; let module_id = Ident . create_persistent module_name in let body , size = Translobj . transl_label_init ( fun ( ) -> transl_struct Location . none [ ] cc ( global_path module_id ) str ) in { module_ident = module_id ; main_module_block_size = size ; required_globals = required_globals ~ flambda : true body ; code = body }
let transl_implementation module_name ( str , cc ) = let implementation = transl_implementation_flambda module_name ( str , cc ) in let code = Lprim ( Psetglobal implementation . module_ident , [ implementation . code ] , Location . none ) in { implementation with code }
let rec defined_idents = function [ ] -> [ ] | item :: rem -> match item . str_desc with | Tstr_eval _ -> defined_idents rem | Tstr_value ( _rec_flag , pat_expr_list ) -> let_bound_idents pat_expr_list @ defined_idents rem | Tstr_primitive _ -> defined_idents rem | Tstr_type _ -> defined_idents rem | Tstr_typext tyext -> List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors @ defined_idents rem | Tstr_exception ext -> ext . tyexn_constructor . ext_id :: defined_idents rem | Tstr_module { mb_id = Some id ; mb_presence = Mp_present } -> id :: defined_idents rem | Tstr_module ( { mb_id = None } { | mb_presence = Mp_absent } ) -> defined_idents rem | Tstr_recmodule decls -> List . filter_map ( fun mb -> mb . mb_id ) decls @ defined_idents rem | Tstr_modtype _ -> defined_idents rem | Tstr_open od -> bound_value_identifiers od . open_bound_items @ defined_idents rem | Tstr_class cl_list -> List . map ( fun ( ci , _ ) -> ci . ci_id_class ) cl_list @ defined_idents rem | Tstr_class_type _ -> defined_idents rem | Tstr_include incl -> bound_value_identifiers incl . incl_type @ defined_idents rem | Tstr_attribute _ -> defined_idents rem
let rec more_idents = function [ ] -> [ ] | item :: rem -> match item . str_desc with | Tstr_eval _ -> more_idents rem | Tstr_value _ -> more_idents rem | Tstr_primitive _ -> more_idents rem | Tstr_type _ -> more_idents rem | Tstr_typext _ -> more_idents rem | Tstr_exception _ -> more_idents rem | Tstr_recmodule _ -> more_idents rem | Tstr_modtype _ -> more_idents rem | Tstr_open od -> let rest = more_idents rem in begin match od . open_expr . mod_desc with | Tmod_structure str -> all_idents str . str_items @ rest | _ -> rest end | Tstr_class _ -> more_idents rem | Tstr_class_type _ -> more_idents rem | Tstr_include { incl_mod { = mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> all_idents str . str_items @ more_idents rem | Tstr_include _ -> more_idents rem | Tstr_module { mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_structure str } } | Tstr_module { mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> all_idents str . str_items @ more_idents rem | Tstr_module _ -> more_idents rem | Tstr_attribute _ -> more_idents rem [ ] -> [ ] | item :: rem -> match item . str_desc with | Tstr_eval _ -> all_idents rem | Tstr_value ( _rec_flag , pat_expr_list ) -> let_bound_idents pat_expr_list @ all_idents rem | Tstr_primitive _ -> all_idents rem | Tstr_type _ -> all_idents rem | Tstr_typext tyext -> List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors @ all_idents rem | Tstr_exception ext -> ext . tyexn_constructor . ext_id :: all_idents rem | Tstr_recmodule decls -> List . filter_map ( fun mb -> mb . mb_id ) decls @ all_idents rem | Tstr_modtype _ -> all_idents rem | Tstr_open od -> let rest = all_idents rem in begin match od . open_expr . mod_desc with | Tmod_structure str -> bound_value_identifiers od . open_bound_items @ all_idents str . str_items @ rest | _ -> bound_value_identifiers od . open_bound_items @ rest end | Tstr_class cl_list -> List . map ( fun ( ci , _ ) -> ci . ci_id_class ) cl_list @ all_idents rem | Tstr_class_type _ -> all_idents rem | Tstr_include { incl_type ; incl_mod { = mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> bound_value_identifiers incl_type @ all_idents str . str_items @ all_idents rem | Tstr_include incl -> bound_value_identifiers incl . incl_type @ all_idents rem | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_structure str } } | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr = { mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> id :: all_idents str . str_items @ all_idents rem | Tstr_module { mb_id = Some id ; mb_presence = Mp_present } -> id :: all_idents rem | Tstr_module ( { mb_id = None } | { mb_presence = Mp_absent } ) -> all_idents rem | Tstr_attribute _ -> all_idents rem
let transl_store_subst = ref Ident . Map . empty
let nat_toplevel_name id = try match Ident . Map . find id ! transl_store_subst with | Lprim ( Pfield pos , [ Lprim ( Pgetglobal glob , [ ] , _ ) ] , _ ) -> ( glob , pos ) | _ -> raise Not_found with Not_found -> fatal_error ( " Translmod . nat_toplevel_name : " ^ Ident . unique_name id )
let field_of_str loc str = let ids = Array . of_list ( defined_idents str . str_items ) in fun ( pos , cc ) -> match cc with | Tcoerce_primitive { pc_loc ; pc_desc ; pc_env ; pc_type ; } -> Translprim . transl_primitive pc_loc pc_desc pc_env pc_type None | Tcoerce_alias ( env , path , cc ) -> let lam = transl_module_path loc env path in apply_coercion loc Alias cc lam | _ -> apply_coercion loc Strict cc ( Lvar ids . ( pos ) )
let transl_store_structure glob map prims aliases str = let no_env_update _ _ env = env in let rec transl_store rootpath subst cont = function [ ] -> transl_store_subst := subst ; Lambda . subst no_env_update subst cont | item :: rem -> match item . str_desc with | Tstr_eval ( expr , _attrs ) -> Lsequence ( Lambda . subst no_env_update subst ( transl_exp expr ) , transl_store rootpath subst cont rem ) | Tstr_value ( rec_flag , pat_expr_list ) -> let ids = let_bound_idents pat_expr_list in let lam = transl_let rec_flag pat_expr_list ( store_idents Location . none ids ) in Lsequence ( Lambda . subst no_env_update subst lam , transl_store rootpath ( add_idents false ids subst ) cont rem ) | Tstr_primitive descr -> record_primitive descr . val_val ; transl_store rootpath subst cont rem | Tstr_type _ -> transl_store rootpath subst cont rem | Tstr_typext ( tyext ) -> let ids = List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors in let lam = transl_type_extension item . str_env rootpath tyext ( store_idents Location . none ids ) in Lsequence ( Lambda . subst no_env_update subst lam , transl_store rootpath ( add_idents false ids subst ) cont rem ) | Tstr_exception ext -> let id = ext . tyexn_constructor . ext_id in let path = field_path rootpath id in let lam = transl_extension_constructor item . str_env path ext . tyexn_constructor in Lsequence ( Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst lam , store_ident ext . tyexn_constructor . ext_loc id ) , transl_store rootpath ( add_ident false id subst ) cont rem ) | Tstr_module { mb_id = None ; mb_name ; mb_presence = Mp_present ; mb_expr = modl ; mb_loc = loc ; mb_attributes } -> let lam = Translattribute . add_inline_attribute ( transl_module Tcoerce_none None modl ) loc mb_attributes in Lsequence ( Lprim ( Pignore , [ lam ] , mb_name . loc ) , transl_store rootpath subst cont rem ) | Tstr_module { mb_id = Some id ; mb_loc = loc ; mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_structure str } as mexp ; mb_attributes } -> List . iter ( Translattribute . check_attribute_on_module mexp ) mb_attributes ; let lam = transl_store ( field_path rootpath id ) subst lambda_unit str . str_items in let subst = ! transl_store_subst in Lsequence ( lam , Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst ( Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun id -> Lvar id ) ( defined_idents str . str_items ) , loc ) ) , Lsequence ( store_ident loc id , transl_store rootpath ( add_ident true id subst ) cont rem ) ) ) | Tstr_module { mb_id = Some id ; mb_loc = loc ; mb_presence = Mp_present ; mb_expr = { mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } as mexp , _ , _ , ( Tcoerce_structure ( map , _ ) as _cc ) ) } ; mb_attributes } -> List . iter ( Translattribute . check_attribute_on_module mexp ) mb_attributes ; let lam = transl_store ( field_path rootpath id ) subst lambda_unit str . str_items in let subst = ! transl_store_subst in let field = field_of_str loc str in Lsequence ( lam , Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst ( Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map field map , loc ) ) , Lsequence ( store_ident loc id , transl_store rootpath ( add_ident true id subst ) cont rem ) ) ) | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr = modl ; mb_loc = loc ; mb_attributes } -> let lam = Translattribute . add_inline_attribute ( transl_module Tcoerce_none ( field_path rootpath id ) modl ) loc mb_attributes in Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst lam , Lsequence ( store_ident loc id , transl_store rootpath ( add_ident true id subst ) cont rem ) ) | Tstr_module { mb_presence = Mp_absent } -> transl_store rootpath subst cont rem | Tstr_recmodule bindings -> let ids = List . filter_map ( fun mb -> mb . mb_id ) bindings in compile_recmodule ( fun id modl _loc -> Lambda . subst no_env_update subst ( transl_module Tcoerce_none ( Option . bind id ( field_path rootpath ) ) modl ) ) bindings ( Lsequence ( store_idents Location . none ids , transl_store rootpath ( add_idents true ids subst ) cont rem ) ) | Tstr_class cl_list -> let ( ids , class_bindings ) = transl_class_bindings cl_list in let lam = Lletrec ( class_bindings , store_idents Location . none ids ) in Lsequence ( Lambda . subst no_env_update subst lam , transl_store rootpath ( add_idents false ids subst ) cont rem ) | Tstr_include { incl_loc = loc ; incl_mod = { mod_desc = Tmod_constraint ( ( { mod_desc = Tmod_structure str } as mexp ) , _ , _ , ( Tcoerce_structure ( map , _ ) ) ) } ; incl_attributes ; incl_type ; } -> List . iter ( Translattribute . check_attribute_on_module mexp ) incl_attributes ; let lam = transl_store None subst lambda_unit str . str_items in let subst = ! transl_store_subst in let field = field_of_str loc str in let ids0 = bound_value_identifiers incl_type in let rec loop ids args = match ids , args with | [ ] , [ ] -> transl_store rootpath ( add_idents true ids0 subst ) cont rem | id :: ids , arg :: args -> Llet ( Alias , Pgenval , id , Lambda . subst no_env_update subst ( field arg ) , Lsequence ( store_ident loc id , loop ids args ) ) | _ -> assert false in Lsequence ( lam , loop ids0 map ) | Tstr_include incl -> let ids = bound_value_identifiers incl . incl_type in let modl = incl . incl_mod in let mid = Ident . create_local " include " in let loc = incl . incl_loc in let rec store_idents pos = function | [ ] -> transl_store rootpath ( add_idents true ids subst ) cont rem | id :: idl -> Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , loc ) , Lsequence ( store_ident loc id , store_idents ( pos + 1 ) idl ) ) in Llet ( Strict , Pgenval , mid , Lambda . subst no_env_update subst ( transl_module Tcoerce_none None modl ) , store_idents 0 ids ) | Tstr_open od -> begin match od . open_expr . mod_desc with | Tmod_structure str -> let lam = transl_store rootpath subst lambda_unit str . str_items in let ids = Array . of_list ( defined_idents str . str_items ) in let ids0 = bound_value_identifiers od . open_bound_items in let subst = ! transl_store_subst in let rec store_idents pos = function | [ ] -> transl_store rootpath ( add_idents true ids0 subst ) cont rem | id :: idl -> Llet ( Alias , Pgenval , id , Lvar ids . ( pos ) , Lsequence ( store_ident od . open_loc id , store_idents ( pos + 1 ) idl ) ) in Lsequence ( lam , Lambda . subst no_env_update subst ( store_idents 0 ids0 ) ) | _ -> let pure = pure_module od . open_expr in match od . open_bound_items with | [ ] when pure = Alias -> transl_store rootpath subst cont rem | _ -> let ids = bound_value_identifiers od . open_bound_items in let mid = Ident . create_local " open " in let loc = od . open_loc in let rec store_idents pos = function [ ] -> transl_store rootpath ( add_idents true ids subst ) cont rem | id :: idl -> Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , loc ) , Lsequence ( store_ident loc id , store_idents ( pos + 1 ) idl ) ) in Llet ( pure , Pgenval , mid , Lambda . subst no_env_update subst ( transl_module Tcoerce_none None od . open_expr ) , store_idents 0 ids ) end | Tstr_modtype _ | Tstr_class_type _ | Tstr_attribute _ -> transl_store rootpath subst cont rem and store_ident loc id = try let ( pos , cc ) = Ident . find_same id map in let init_val = apply_coercion loc Alias cc ( Lvar id ) in Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , loc ) ; init_val ] , loc ) with Not_found -> fatal_error ( " Translmod . store_ident : " ^ Ident . unique_name id ) and store_idents loc idlist = make_sequence ( store_ident loc ) idlist and add_ident may_coerce id subst = try let ( pos , cc ) = Ident . find_same id map in match cc with Tcoerce_none -> Ident . Map . add id ( Lprim ( Pfield pos , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ] , Location . none ) ) subst | _ -> if may_coerce then subst else assert false with Not_found -> assert false and add_idents may_coerce idlist subst = List . fold_right ( add_ident may_coerce ) idlist subst and store_primitive ( pos , prim ) cont = Lsequence ( Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ; Translprim . transl_primitive Location . none prim . pc_desc prim . pc_env prim . pc_type None ] , Location . none ) , cont ) and store_alias ( pos , env , path , cc ) = let path_lam = transl_module_path Location . none env path in let init_val = apply_coercion Location . none Strict cc path_lam in Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ; init_val ] , Location . none ) in let aliases = make_sequence store_alias aliases in List . fold_right store_primitive prims ( transl_store ( global_path glob ) ! transl_store_subst aliases str )
let build_ident_map restr idlist more_ids = let rec natural_map pos map prims aliases = function | [ ] -> ( map , prims , aliases , pos ) | id :: rem -> natural_map ( pos + 1 ) ( Ident . add id ( pos , Tcoerce_none ) map ) prims aliases rem in let ( map , prims , aliases , pos ) = match restr with | Tcoerce_none -> natural_map 0 Ident . empty [ ] [ ] idlist | Tcoerce_structure ( pos_cc_list , _id_pos_list ) -> let idarray = Array . of_list idlist in let rec export_map pos map prims aliases undef = function | [ ] -> natural_map pos map prims aliases undef | ( _source_pos , Tcoerce_primitive p ) :: rem -> export_map ( pos + 1 ) map ( ( pos , p ) :: prims ) aliases undef rem | ( _source_pos , Tcoerce_alias ( env , path , cc ) ) :: rem -> export_map ( pos + 1 ) map prims ( ( pos , env , path , cc ) :: aliases ) undef rem | ( source_pos , cc ) :: rem -> let id = idarray . ( source_pos ) in export_map ( pos + 1 ) ( Ident . add id ( pos , cc ) map ) prims aliases ( list_remove id undef ) rem in export_map 0 Ident . empty [ ] [ ] idlist pos_cc_list | _ -> fatal_error " Translmod . build_ident_map " in natural_map pos map prims aliases more_ids
let transl_store_gen module_name ( { str_items = str } , restr ) topl = reset_labels ( ) ; primitive_declarations := [ ] ; Translprim . clear_used_primitives ( ) ; let module_id = Ident . create_persistent module_name in let ( map , prims , aliases , size ) = build_ident_map restr ( defined_idents str ) ( more_idents str ) in let f = function | [ { str_desc = Tstr_eval ( expr , _attrs ) } ] when topl -> assert ( size = 0 ) ; Lambda . subst ( fun _ _ env -> env ) ! transl_store_subst ( transl_exp expr ) | str -> transl_store_structure module_id map prims aliases str in transl_store_label_init module_id size f str
let transl_store_phrases module_name str = transl_store_gen module_name ( str , Tcoerce_none ) true
let transl_store_implementation module_name ( str , restr ) = let s = ! transl_store_subst in transl_store_subst := Ident . Map . empty ; let ( i , code ) = transl_store_gen module_name ( str , restr ) false in transl_store_subst := s ; { Lambda . main_module_block_size = i ; code ; module_ident = Ident . create_persistent module_name ; required_globals = required_globals ~ flambda : true code }
let toploop_ident = Ident . create_persistent " Toploop "
let aliased_idents = ref Ident . empty
let set_toplevel_unique_name id = aliased_idents := Ident . add id ( Ident . unique_toplevel_name id ) ! aliased_idents
let toplevel_name id = try Ident . find_same id ! aliased_idents with Not_found -> Ident . name id
let toploop_getvalue id = Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = Lprim ( Pfield toploop_getvalue_pos , [ Lprim ( Pgetglobal toploop_ident , [ ] , Location . none ) ] , Location . none ) ; ap_args [ = Lconst ( Const_base ( Const_string ( toplevel_name id , None ) ) ) ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise }
let toploop_setvalue id lam = Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = Lprim ( Pfield toploop_setvalue_pos , [ Lprim ( Pgetglobal toploop_ident , [ ] , Location . none ) ] , Location . none ) ; ap_args [ = Lconst ( Const_base ( Const_string ( toplevel_name id , None ) ) ) ; lam ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise }
let toploop_setvalue_id id = toploop_setvalue id ( Lvar id )
let close_toplevel_term ( lam , ( ) ) = Ident . Set . fold ( fun id l -> Llet ( Strict , Pgenval , id , toploop_getvalue id , l ) ) ( free_variables lam ) lam
let transl_toplevel_item item = match item . str_desc with Tstr_eval ( expr , _ ) | Tstr_value ( Nonrecursive , [ { vb_pat = { pat_desc = Tpat_any } ; vb_expr = expr } ] ) -> transl_exp expr | Tstr_value ( rec_flag , pat_expr_list ) -> let idents = let_bound_idents pat_expr_list in transl_let rec_flag pat_expr_list ( make_sequence toploop_setvalue_id idents ) | Tstr_typext ( tyext ) -> let idents = List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors in List . iter set_toplevel_unique_name idents ; transl_type_extension item . str_env None tyext ( make_sequence toploop_setvalue_id idents ) | Tstr_exception ext -> set_toplevel_unique_name ext . tyexn_constructor . ext_id ; toploop_setvalue ext . tyexn_constructor . ext_id ( transl_extension_constructor item . str_env None ext . tyexn_constructor ) | Tstr_module { mb_id = None ; mb_presence = Mp_present ; mb_expr = modl } -> transl_module Tcoerce_none None modl | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr = modl } -> set_toplevel_unique_name id ; let lam = transl_module Tcoerce_none ( Some ( Pident id ) ) modl in toploop_setvalue id lam | Tstr_recmodule bindings -> let idents = List . filter_map ( fun mb -> mb . mb_id ) bindings in compile_recmodule ( fun id modl _loc -> transl_module Tcoerce_none ( Option . map ( fun i -> Pident i ) id ) modl ) bindings ( make_sequence toploop_setvalue_id idents ) | Tstr_class cl_list -> let ( ids , class_bindings ) = transl_class_bindings cl_list in List . iter set_toplevel_unique_name ids ; Lletrec ( class_bindings , make_sequence toploop_setvalue_id ids ) | Tstr_include incl -> let ids = bound_value_identifiers incl . incl_type in let modl = incl . incl_mod in let mid = Ident . create_local " include " in let rec set_idents pos = function [ ] -> lambda_unit | id :: ids -> Lsequence ( toploop_setvalue id ( Lprim ( Pfield pos , [ Lvar mid ] , Location . none ) ) , set_idents ( pos + 1 ) ids ) in Llet ( Strict , Pgenval , mid , transl_module Tcoerce_none None modl , set_idents 0 ids ) | Tstr_primitive descr -> record_primitive descr . val_val ; lambda_unit | Tstr_open od -> let pure = pure_module od . open_expr in begin match od . open_bound_items with | [ ] when pure = Alias -> lambda_unit | _ -> let ids = bound_value_identifiers od . open_bound_items in let mid = Ident . create_local " open " in let rec set_idents pos = function [ ] -> lambda_unit | id :: ids -> Lsequence ( toploop_setvalue id ( Lprim ( Pfield pos , [ Lvar mid ] , Location . none ) ) , set_idents ( pos + 1 ) ids ) in Llet ( pure , Pgenval , mid , transl_module Tcoerce_none None od . open_expr , set_idents 0 ids ) end | Tstr_modtype _ | Tstr_module { mb_presence = Mp_absent } | Tstr_type _ | Tstr_class_type _ | Tstr_attribute _ -> lambda_unit
let transl_toplevel_item_and_close itm = close_toplevel_term ( transl_label_init ( fun ( ) -> transl_toplevel_item itm , ( ) ) )
let transl_toplevel_definition str = reset_labels ( ) ; Translprim . clear_used_primitives ( ) ; make_sequence transl_toplevel_item_and_close str . str_items
let get_component = function None -> Lconst const_unit | Some id -> Lprim ( Pgetglobal id , [ ] , Location . none )
let transl_package_flambda component_names coercion = let size = match coercion with | Tcoerce_none -> List . length component_names | Tcoerce_structure ( l , _ ) -> List . length l | Tcoerce_functor _ | Tcoerce_primitive _ | Tcoerce_alias _ -> assert false in size , apply_coercion Location . none Strict coercion ( Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map get_component component_names , Location . none ) )
let transl_package component_names target_name coercion = let components = Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map get_component component_names , Location . none ) in Lprim ( Psetglobal target_name , [ apply_coercion Location . none Strict coercion components ] , Location . none ) let g = Array . of_list component_names in List . map ( fun ( pos , cc ) -> apply_coercion Strict cc ( get_component g . ( pos ) ) ) pos_cc_list | _ -> assert false in Lprim ( Psetglobal target_name , [ Lprim ( Pmakeblock ( 0 , Immutable ) , components ) ] ) ) *
let transl_store_package component_names target_name coercion = let rec make_sequence fn pos arg = match arg with [ ] -> lambda_unit | hd :: tl -> Lsequence ( fn pos hd , make_sequence fn ( pos + 1 ) tl ) in match coercion with Tcoerce_none -> ( List . length component_names , make_sequence ( fun pos id -> Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal target_name , [ ] , Location . none ) ; get_component id ] , Location . none ) ) 0 component_names ) | Tcoerce_structure ( pos_cc_list , _id_pos_list ) -> let components = Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map get_component component_names , Location . none ) in let blk = Ident . create_local " block " in ( List . length pos_cc_list , Llet ( Strict , Pgenval , blk , apply_coercion Location . none Strict coercion components , make_sequence ( fun pos _id -> Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal target_name , [ ] , Location . none ) ; Lprim ( Pfield pos , [ Lvar blk ] , Location . none ) ] , Location . none ) ) 0 pos_cc_list ) ) let id = Array . of_list component_names in ( List . length pos_cc_list , make_sequence ( fun dst ( src , cc ) -> Lprim ( Psetfield ( dst , false ) , [ Lprim ( Pgetglobal target_name , [ ] ) ; apply_coercion Strict cc ( get_component id . ( src ) ) ] ) ) 0 pos_cc_list ) ) * | _ -> assert false
let print_cycle ppf cycle = let print_ident ppf ( x , _ ) = Format . pp_print_string ppf ( Ident . name x ) in let pp_sep ppf ( ) = fprintf ppf " @ -> " in Format . fprintf ppf " % a % a % s " ( Format . pp_print_list ~ pp_sep print_ident ) cycle pp_sep ( ) ( Ident . name @@ fst @@ List . hd cycle )
let explanation_submsg ( id , unsafe_info ) = match unsafe_info with | Unnamed -> assert false | Unsafe { reason ; loc ; subid } -> let print fmt = let printer = Format . dprintf fmt ( Ident . name id ) ( Ident . name subid ) in Location . mkloc printer loc in match reason with | Unsafe_module_binding -> print " Module % s defines an unsafe module , % s . " | Unsafe_functor -> print " Module % s defines an unsafe functor , % s . " | Unsafe_typext -> print " Module % s defines an unsafe extension constructor , % s . " | Unsafe_non_function -> print " Module % s defines an unsafe value , % s . "
let report_error loc = function | Circular_dependency cycle -> let [ @ manual . ref " s : recursive - modules " ] chapter , section = 8 , 2 in Location . errorf ~ loc ~ sub ( : List . map explanation_submsg cycle ) " Cannot safely evaluate the definition of the following cycle @ \ of recursively - defined modules :@ % a . @ \ There are no safe modules in this cycle @ ( see manual section % d . % d ) . " print_cycle cycle chapter section | Conflicting_inline_attributes -> Location . errorf " [ @ Conflicting ' inline ' attributes ] " @
let ( ) = Location . register_error_of_exn ( function | Error ( loc , err ) -> Some ( report_error loc err ) | _ -> None )
let reset ( ) = primitive_declarations := [ ] ; transl_store_subst := Ident . Map . empty ; aliased_idents := Ident . empty ; Env . reset_required_globals ( ) ; Translprim . clear_used_primitives ( )
let oo_prim = Lambda . transl_prim " CamlinternalOO "
let consts : ( structured_constant , Ident . t ) Hashtbl . t = Hashtbl . create 17
let share c = match c with Const_block ( _n , l ) when l <> [ ] -> begin try Lvar ( Hashtbl . find consts c ) with Not_found -> let id = Ident . create_local " shared " in Hashtbl . add consts c id ; Lvar id end | _ -> Lconst c
let cache_required = ref false
let method_cache = ref lambda_unit
let method_count = ref 0
let method_table = ref [ ]
let meth_tag s = Lconst ( Const_base ( Const_int ( Btype . hash_variant s ) ) )
let next_cache tag = let n = ! method_count in incr method_count ; ( tag , [ ! method_cache ; Lconst ( Const_base ( Const_int n ) ) ] )
let rec is_path = function Lvar _ | Lprim ( Pgetglobal _ , [ ] , _ ) | Lconst _ -> true | Lprim ( Pfield _ , [ lam ] , _ ) -> is_path lam | Lprim ( ( Parrayrefu _ | Parrayrefs _ ) , [ lam1 ; lam2 ] , _ ) -> is_path lam1 && is_path lam2 | _ -> false
let meth obj lab = let tag = meth_tag lab in if not ( ! cache_required && ! Clflags . native_code ) then ( tag , [ ] ) else if not ( is_path obj ) then next_cache tag else try let r = List . assoc obj ! method_table in try ( tag , List . assoc tag ! r ) with Not_found -> let p = next_cache tag in r := p :: ! r ; p with Not_found -> let p = next_cache tag in method_table := ( obj , ref [ p ] ) :: ! method_table ; p
let reset_labels ( ) = Hashtbl . clear consts ; method_count := 0 ; method_table := [ ]
let int n = Lconst ( Const_base ( Const_int n ) )
let prim_makearray = Primitive . simple ~ name " : caml_make_vect " ~ arity : 2 ~ alloc : true
let transl_label_init_general f = let expr , size = f ( ) in let expr = Hashtbl . fold ( fun c id expr -> Llet ( Alias , Pgenval , id , Lconst c , expr ) ) consts expr in reset_labels ( ) ; expr , size
let transl_label_init_flambda f = assert ( Config . flambda ) ; let method_cache_id = Ident . create_local " method_cache " in method_cache := Lvar method_cache_id ; let expr , size = f ( ) in let expr = if ! method_count = 0 then expr else Llet ( Strict , Pgenval , method_cache_id , Lprim ( Pccall prim_makearray , [ int ! method_count ; int 0 ] , Location . none ) , expr ) in transl_label_init_general ( fun ( ) -> expr , size )
let transl_store_label_init glob size f arg = assert ( not Config . flambda ) ; assert ( ! Clflags . native_code ) ; method_cache := Lprim ( Pfield size , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ] , Location . none ) ; let expr = f arg in let ( size , expr ) = if ! method_count = 0 then ( size , expr ) else ( size + 1 , Lsequence ( Lprim ( Psetfield ( size , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ; Lprim ( Pccall prim_makearray , [ int ! method_count ; int 0 ] , Location . none ) ] , Location . none ) , expr ) ) in let lam , size = transl_label_init_general ( fun ( ) -> ( expr , size ) ) in size , lam
let transl_label_init f = if ! Clflags . native_code then transl_label_init_flambda f else transl_label_init_general f
let wrapping = ref false
let top_env = ref Env . empty
let classes = ref [ ]
let method_ids = ref Ident . Set . empty
let oo_add_class id = classes := id :: ! classes ; ( ! top_env , ! cache_required )
let oo_wrap env req f x = if ! wrapping then if ! cache_required then f x else Misc . protect_refs [ Misc . R ( cache_required , true ) ] ( fun ( ) -> f x ) else Misc . protect_refs [ Misc . R ( wrapping , true ) ; Misc . R ( top_env , env ) ] ( fun ( ) -> cache_required := req ; classes := [ ] ; method_ids := Ident . Set . empty ; let lambda = f x in let lambda = List . fold_left ( fun lambda id -> Llet ( StrictOpt , Pgenval , id , Lprim ( Pmakeblock ( 0 , Mutable , None ) , [ lambda_unit ; lambda_unit ; lambda_unit ] , Location . none ) , lambda ) ) lambda ! classes in lambda )
let reset ( ) = Hashtbl . clear consts ; cache_required := false ; method_cache := lambda_unit ; method_count := 0 ; method_table := [ ] ; wrapping := false ; top_env := Env . empty ; classes := [ ] ; method_ids := Ident . Set . empty
type error = | Unknown_builtin_primitive of string | Wrong_arity_builtin_primitive of string
let event_before exp lam = match lam with if ! Clflags . debug && not ! Clflags . native_code then Levent ( lam , { lev_loc = exp . exp_loc ; lev_kind = Lev_before ; lev_repr = None ; lev_env = exp . exp_env } ) else lam
let event_after exp lam = if ! Clflags . debug && not ! Clflags . native_code then Levent ( lam , { lev_loc = exp . exp_loc ; lev_kind = Lev_after exp . exp_type ; lev_repr = None ; lev_env = exp . exp_env } ) else lam
type comparison = | Equal | Not_equal | Less_equal | Less_than | Greater_equal | Greater_than | Compare
type comparison_kind = | Compare_generic | Compare_ints | Compare_floats | Compare_strings | Compare_bytes | Compare_nativeints | Compare_int32s | Compare_int64s
type loc_kind = | Loc_FILE | Loc_LINE | Loc_MODULE | Loc_LOC | Loc_POS
type prim = | Primitive of Lambda . primitive * int | External of Primitive . description | Comparison of comparison * comparison_kind | Raise of Lambda . raise_kind | Raise_with_backtrace | Lazy_force | Loc of loc_kind | Send | Send_self | Send_cache
let used_primitives = Hashtbl . create 7
let add_used_primitive loc env path = match path with Some ( Path . Pdot _ as path ) -> let path = Env . normalize_path_prefix ( Some loc ) env path in let unit = Path . head path in if Ident . global unit && not ( Hashtbl . mem used_primitives path ) then Hashtbl . add used_primitives path loc | _ -> ( )
let clear_used_primitives ( ) = Hashtbl . clear used_primitives
let get_used_primitives ( ) = Hashtbl . fold ( fun path _ acc -> path :: acc ) used_primitives [ ]
let gen_array_kind = if Config . flat_float_array then Pgenarray else Paddrarray
let prim_sys_argv = Primitive . simple ~ name " : caml_sys_argv " ~ arity : 1 ~ alloc : true
let primitives_table = create_hashtable 57 [ " % identity " , Primitive ( Pidentity , 1 ) ; " % bytes_to_string " , Primitive ( Pbytes_to_string , 1 ) ; " % bytes_of_string " , Primitive ( Pbytes_of_string , 1 ) ; " % ignore " , Primitive ( Pignore , 1 ) ; " % revapply " , Primitive ( Prevapply , 2 ) ; " % apply " , Primitive ( Pdirapply , 2 ) ; " % loc_LOC " , Loc Loc_LOC ; " % loc_FILE " , Loc Loc_FILE ; " % loc_LINE " , Loc Loc_LINE ; " % loc_POS " , Loc Loc_POS ; " % loc_MODULE " , Loc Loc_MODULE ; " % field0 " , Primitive ( ( Pfield 0 ) , 1 ) ; " % field1 " , Primitive ( ( Pfield 1 ) , 1 ) ; " % setfield0 " , Primitive ( ( Psetfield ( 0 , Pointer , Assignment ) ) , 2 ) ; " % makeblock " , Primitive ( ( Pmakeblock ( 0 , Immutable , None ) ) , 1 ) ; " % makemutable " , Primitive ( ( Pmakeblock ( 0 , Mutable , None ) ) , 1 ) ; " % raise " , Raise Raise_regular ; " % reraise " , Raise Raise_reraise ; " % raise_notrace " , Raise Raise_notrace ; " % raise_with_backtrace " , Raise_with_backtrace ; " % sequand " , Primitive ( Psequand , 2 ) ; " % sequor " , Primitive ( Psequor , 2 ) ; " % boolnot " , Primitive ( Pnot , 1 ) ; " % big_endian " , Primitive ( ( Pctconst Big_endian ) , 1 ) ; " % backend_type " , Primitive ( ( Pctconst Backend_type ) , 1 ) ; " % word_size " , Primitive ( ( Pctconst Word_size ) , 1 ) ; " % int_size " , Primitive ( ( Pctconst Int_size ) , 1 ) ; " % max_wosize " , Primitive ( ( Pctconst Max_wosize ) , 1 ) ; " % ostype_unix " , Primitive ( ( Pctconst Ostype_unix ) , 1 ) ; " % ostype_win32 " , Primitive ( ( Pctconst Ostype_win32 ) , 1 ) ; " % ostype_cygwin " , Primitive ( ( Pctconst Ostype_cygwin ) , 1 ) ; " % negint " , Primitive ( Pnegint , 1 ) ; " % succint " , Primitive ( ( Poffsetint 1 ) , 1 ) ; " % predint " , Primitive ( ( Poffsetint ( - 1 ) ) , 1 ) ; " % addint " , Primitive ( Paddint , 2 ) ; " % subint " , Primitive ( Psubint , 2 ) ; " % mulint " , Primitive ( Pmulint , 2 ) ; " % divint " , Primitive ( ( Pdivint Safe ) , 2 ) ; " % modint " , Primitive ( ( Pmodint Safe ) , 2 ) ; " % andint " , Primitive ( Pandint , 2 ) ; " % orint " , Primitive ( Porint , 2 ) ; " % xorint " , Primitive ( Pxorint , 2 ) ; " % lslint " , Primitive ( Plslint , 2 ) ; " % lsrint " , Primitive ( Plsrint , 2 ) ; " % asrint " , Primitive ( Pasrint , 2 ) ; " % eq " , Primitive ( ( Pintcomp Ceq ) , 2 ) ; " % noteq " , Primitive ( ( Pintcomp Cne ) , 2 ) ; " % ltint " , Primitive ( ( Pintcomp Clt ) , 2 ) ; " % leint " , Primitive ( ( Pintcomp Cle ) , 2 ) ; " % gtint " , Primitive ( ( Pintcomp Cgt ) , 2 ) ; " % geint " , Primitive ( ( Pintcomp Cge ) , 2 ) ; " % incr " , Primitive ( ( Poffsetref ( 1 ) ) , 1 ) ; " % decr " , Primitive ( ( Poffsetref ( - 1 ) ) , 1 ) ; " % intoffloat " , Primitive ( Pintoffloat , 1 ) ; " % floatofint " , Primitive ( Pfloatofint , 1 ) ; " % negfloat " , Primitive ( Pnegfloat , 1 ) ; " % absfloat " , Primitive ( Pabsfloat , 1 ) ; " % addfloat " , Primitive ( Paddfloat , 2 ) ; " % subfloat " , Primitive ( Psubfloat , 2 ) ; " % mulfloat " , Primitive ( Pmulfloat , 2 ) ; " % divfloat " , Primitive ( Pdivfloat , 2 ) ; " % eqfloat " , Primitive ( ( Pfloatcomp CFeq ) , 2 ) ; " % noteqfloat " , Primitive ( ( Pfloatcomp CFneq ) , 2 ) ; " % ltfloat " , Primitive ( ( Pfloatcomp CFlt ) , 2 ) ; " % lefloat " , Primitive ( ( Pfloatcomp CFle ) , 2 ) ; " % gtfloat " , Primitive ( ( Pfloatcomp CFgt ) , 2 ) ; " % gefloat " , Primitive ( ( Pfloatcomp CFge ) , 2 ) ; " % string_length " , Primitive ( Pstringlength , 1 ) ; " % string_safe_get " , Primitive ( Pstringrefs , 2 ) ; " % string_safe_set " , Primitive ( Pbytessets , 3 ) ; " % string_unsafe_get " , Primitive ( Pstringrefu , 2 ) ; " % string_unsafe_set " , Primitive ( Pbytessetu , 3 ) ; " % bytes_length " , Primitive ( Pbyteslength , 1 ) ; " % bytes_safe_get " , Primitive ( Pbytesrefs , 2 ) ; " % bytes_safe_set " , Primitive ( Pbytessets , 3 ) ; " % bytes_unsafe_get " , Primitive ( Pbytesrefu , 2 ) ; " % bytes_unsafe_set " , Primitive ( Pbytessetu , 3 ) ; " % array_length " , Primitive ( ( Parraylength gen_array_kind ) , 1 ) ; " % array_safe_get " , Primitive ( ( Parrayrefs gen_array_kind ) , 2 ) ; " % array_safe_set " , Primitive ( ( Parraysets gen_array_kind ) , 3 ) ; " % array_unsafe_get " , Primitive ( ( Parrayrefu gen_array_kind ) , 2 ) ; " % array_unsafe_set " , Primitive ( ( Parraysetu gen_array_kind ) , 3 ) ; " % obj_size " , Primitive ( ( Parraylength gen_array_kind ) , 1 ) ; " % obj_field " , Primitive ( ( Parrayrefu gen_array_kind ) , 2 ) ; " % obj_set_field " , Primitive ( ( Parraysetu gen_array_kind ) , 3 ) ; " % floatarray_length " , Primitive ( ( Parraylength Pfloatarray ) , 1 ) ; " % floatarray_safe_get " , Primitive ( ( Parrayrefs Pfloatarray ) , 2 ) ; " % floatarray_safe_set " , Primitive ( ( Parraysets Pfloatarray ) , 3 ) ; " % floatarray_unsafe_get " , Primitive ( ( Parrayrefu Pfloatarray ) , 2 ) ; " % floatarray_unsafe_set " , Primitive ( ( Parraysetu Pfloatarray ) , 3 ) ; " % obj_is_int " , Primitive ( Pisint , 1 ) ; " % lazy_force " , Lazy_force ; " % nativeint_of_int " , Primitive ( ( Pbintofint Pnativeint ) , 1 ) ; " % nativeint_to_int " , Primitive ( ( Pintofbint Pnativeint ) , 1 ) ; " % nativeint_neg " , Primitive ( ( Pnegbint Pnativeint ) , 1 ) ; " % nativeint_add " , Primitive ( ( Paddbint Pnativeint ) , 2 ) ; " % nativeint_sub " , Primitive ( ( Psubbint Pnativeint ) , 2 ) ; " % nativeint_mul " , Primitive ( ( Pmulbint Pnativeint ) , 2 ) ; " % nativeint_div " , Primitive ( ( Pdivbint { size = Pnativeint ; is_safe = Safe } ) , 2 ) ; " % nativeint_mod " , Primitive ( ( Pmodbint { size = Pnativeint ; is_safe = Safe } ) , 2 ) ; " % nativeint_and " , Primitive ( ( Pandbint Pnativeint ) , 2 ) ; " % nativeint_or " , Primitive ( ( Porbint Pnativeint ) , 2 ) ; " % nativeint_xor " , Primitive ( ( Pxorbint Pnativeint ) , 2 ) ; " % nativeint_lsl " , Primitive ( ( Plslbint Pnativeint ) , 2 ) ; " % nativeint_lsr " , Primitive ( ( Plsrbint Pnativeint ) , 2 ) ; " % nativeint_asr " , Primitive ( ( Pasrbint Pnativeint ) , 2 ) ; " % int32_of_int " , Primitive ( ( Pbintofint Pint32 ) , 1 ) ; " % int32_to_int " , Primitive ( ( Pintofbint Pint32 ) , 1 ) ; " % int32_neg " , Primitive ( ( Pnegbint Pint32 ) , 1 ) ; " % int32_add " , Primitive ( ( Paddbint Pint32 ) , 2 ) ; " % int32_sub " , Primitive ( ( Psubbint Pint32 ) , 2 ) ; " % int32_mul " , Primitive ( ( Pmulbint Pint32 ) , 2 ) ; " % int32_div " , Primitive ( ( Pdivbint { size = Pint32 ; is_safe = Safe } ) , 2 ) ; " % int32_mod " , Primitive ( ( Pmodbint { size = Pint32 ; is_safe = Safe } ) , 2 ) ; " % int32_and " , Primitive ( ( Pandbint Pint32 ) , 2 ) ; " % int32_or " , Primitive ( ( Porbint Pint32 ) , 2 ) ; " % int32_xor " , Primitive ( ( Pxorbint Pint32 ) , 2 ) ; " % int32_lsl " , Primitive ( ( Plslbint Pint32 ) , 2 ) ; " % int32_lsr " , Primitive ( ( Plsrbint Pint32 ) , 2 ) ; " % int32_asr " , Primitive ( ( Pasrbint Pint32 ) , 2 ) ; " % int64_of_int " , Primitive ( ( Pbintofint Pint64 ) , 1 ) ; " % int64_to_int " , Primitive ( ( Pintofbint Pint64 ) , 1 ) ; " % int64_neg " , Primitive ( ( Pnegbint Pint64 ) , 1 ) ; " % int64_add " , Primitive ( ( Paddbint Pint64 ) , 2 ) ; " % int64_sub " , Primitive ( ( Psubbint Pint64 ) , 2 ) ; " % int64_mul " , Primitive ( ( Pmulbint Pint64 ) , 2 ) ; " % int64_div " , Primitive ( ( Pdivbint { size = Pint64 ; is_safe = Safe } ) , 2 ) ; " % int64_mod " , Primitive ( ( Pmodbint { size = Pint64 ; is_safe = Safe } ) , 2 ) ; " % int64_and " , Primitive ( ( Pandbint Pint64 ) , 2 ) ; " % int64_or " , Primitive ( ( Porbint Pint64 ) , 2 ) ; " % int64_xor " , Primitive ( ( Pxorbint Pint64 ) , 2 ) ; " % int64_lsl " , Primitive ( ( Plslbint Pint64 ) , 2 ) ; " % int64_lsr " , Primitive ( ( Plsrbint Pint64 ) , 2 ) ; " % int64_asr " , Primitive ( ( Pasrbint Pint64 ) , 2 ) ; " % nativeint_of_int32 " , Primitive ( ( Pcvtbint ( Pint32 , Pnativeint ) ) , 1 ) ; " % nativeint_to_int32 " , Primitive ( ( Pcvtbint ( Pnativeint , Pint32 ) ) , 1 ) ; " % int64_of_int32 " , Primitive ( ( Pcvtbint ( Pint32 , Pint64 ) ) , 1 ) ; " % int64_to_int32 " , Primitive ( ( Pcvtbint ( Pint64 , Pint32 ) ) , 1 ) ; " % int64_of_nativeint " , Primitive ( ( Pcvtbint ( Pnativeint , Pint64 ) ) , 1 ) ; " % int64_to_nativeint " , Primitive ( ( Pcvtbint ( Pint64 , Pnativeint ) ) , 1 ) ; " % caml_ba_ref_1 " , Primitive ( ( Pbigarrayref ( false , 1 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 2 ) ; " % caml_ba_ref_2 " , Primitive ( ( Pbigarrayref ( false , 2 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 3 ) ; " % caml_ba_ref_3 " , Primitive ( ( Pbigarrayref ( false , 3 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 4 ) ; " % caml_ba_set_1 " , Primitive ( ( Pbigarrayset ( false , 1 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 3 ) ; " % caml_ba_set_2 " , Primitive ( ( Pbigarrayset ( false , 2 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 4 ) ; " % caml_ba_set_3 " , Primitive ( ( Pbigarrayset ( false , 3 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 5 ) ; " % caml_ba_unsafe_ref_1 " , Primitive ( ( Pbigarrayref ( true , 1 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 2 ) ; " % caml_ba_unsafe_ref_2 " , Primitive ( ( Pbigarrayref ( true , 2 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 3 ) ; " % caml_ba_unsafe_ref_3 " , Primitive ( ( Pbigarrayref ( true , 3 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 4 ) ; " % caml_ba_unsafe_set_1 " , Primitive ( ( Pbigarrayset ( true , 1 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 3 ) ; " % caml_ba_unsafe_set_2 " , Primitive ( ( Pbigarrayset ( true , 2 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 4 ) ; " % caml_ba_unsafe_set_3 " , Primitive ( ( Pbigarrayset ( true , 3 , Pbigarray_unknown , Pbigarray_unknown_layout ) ) , 5 ) ; " % caml_ba_dim_1 " , Primitive ( ( Pbigarraydim ( 1 ) ) , 1 ) ; " % caml_ba_dim_2 " , Primitive ( ( Pbigarraydim ( 2 ) ) , 1 ) ; " % caml_ba_dim_3 " , Primitive ( ( Pbigarraydim ( 3 ) ) , 1 ) ; " % caml_string_get16 " , Primitive ( ( Pstring_load_16 ( false ) ) , 2 ) ; " % caml_string_get16u " , Primitive ( ( Pstring_load_16 ( true ) ) , 2 ) ; " % caml_string_get32 " , Primitive ( ( Pstring_load_32 ( false ) ) , 2 ) ; " % caml_string_get32u " , Primitive ( ( Pstring_load_32 ( true ) ) , 2 ) ; " % caml_string_get64 " , Primitive ( ( Pstring_load_64 ( false ) ) , 2 ) ; " % caml_string_get64u " , Primitive ( ( Pstring_load_64 ( true ) ) , 2 ) ; " % caml_string_set16 " , Primitive ( ( Pbytes_set_16 ( false ) ) , 3 ) ; " % caml_string_set16u " , Primitive ( ( Pbytes_set_16 ( true ) ) , 3 ) ; " % caml_string_set32 " , Primitive ( ( Pbytes_set_32 ( false ) ) , 3 ) ; " % caml_string_set32u " , Primitive ( ( Pbytes_set_32 ( true ) ) , 3 ) ; " % caml_string_set64 " , Primitive ( ( Pbytes_set_64 ( false ) ) , 3 ) ; " % caml_string_set64u " , Primitive ( ( Pbytes_set_64 ( true ) ) , 3 ) ; " % caml_bytes_get16 " , Primitive ( ( Pbytes_load_16 ( false ) ) , 2 ) ; " % caml_bytes_get16u " , Primitive ( ( Pbytes_load_16 ( true ) ) , 2 ) ; " % caml_bytes_get32 " , Primitive ( ( Pbytes_load_32 ( false ) ) , 2 ) ; " % caml_bytes_get32u " , Primitive ( ( Pbytes_load_32 ( true ) ) , 2 ) ; " % caml_bytes_get64 " , Primitive ( ( Pbytes_load_64 ( false ) ) , 2 ) ; " % caml_bytes_get64u " , Primitive ( ( Pbytes_load_64 ( true ) ) , 2 ) ; " % caml_bytes_set16 " , Primitive ( ( Pbytes_set_16 ( false ) ) , 3 ) ; " % caml_bytes_set16u " , Primitive ( ( Pbytes_set_16 ( true ) ) , 3 ) ; " % caml_bytes_set32 " , Primitive ( ( Pbytes_set_32 ( false ) ) , 3 ) ; " % caml_bytes_set32u " , Primitive ( ( Pbytes_set_32 ( true ) ) , 3 ) ; " % caml_bytes_set64 " , Primitive ( ( Pbytes_set_64 ( false ) ) , 3 ) ; " % caml_bytes_set64u " , Primitive ( ( Pbytes_set_64 ( true ) ) , 3 ) ; " % caml_bigstring_get16 " , Primitive ( ( Pbigstring_load_16 ( false ) ) , 2 ) ; " % caml_bigstring_get16u " , Primitive ( ( Pbigstring_load_16 ( true ) ) , 2 ) ; " % caml_bigstring_get32 " , Primitive ( ( Pbigstring_load_32 ( false ) ) , 2 ) ; " % caml_bigstring_get32u " , Primitive ( ( Pbigstring_load_32 ( true ) ) , 2 ) ; " % caml_bigstring_get64 " , Primitive ( ( Pbigstring_load_64 ( false ) ) , 2 ) ; " % caml_bigstring_get64u " , Primitive ( ( Pbigstring_load_64 ( true ) ) , 2 ) ; " % caml_bigstring_set16 " , Primitive ( ( Pbigstring_set_16 ( false ) ) , 3 ) ; " % caml_bigstring_set16u " , Primitive ( ( Pbigstring_set_16 ( true ) ) , 3 ) ; " % caml_bigstring_set32 " , Primitive ( ( Pbigstring_set_32 ( false ) ) , 3 ) ; " % caml_bigstring_set32u " , Primitive ( ( Pbigstring_set_32 ( true ) ) , 3 ) ; " % caml_bigstring_set64 " , Primitive ( ( Pbigstring_set_64 ( false ) ) , 3 ) ; " % caml_bigstring_set64u " , Primitive ( ( Pbigstring_set_64 ( true ) ) , 3 ) ; " % bswap16 " , Primitive ( Pbswap16 , 1 ) ; " % bswap_int32 " , Primitive ( ( Pbbswap ( Pint32 ) ) , 1 ) ; " % bswap_int64 " , Primitive ( ( Pbbswap ( Pint64 ) ) , 1 ) ; " % bswap_native " , Primitive ( ( Pbbswap ( Pnativeint ) ) , 1 ) ; " % int_as_pointer " , Primitive ( Pint_as_pointer , 1 ) ; " % opaque " , Primitive ( Popaque , 1 ) ; " % sys_argv " , External prim_sys_argv ; " % send " , Send ; " % sendself " , Send_self ; " % sendcache " , Send_cache ; " % equal " , Comparison ( Equal , Compare_generic ) ; " % notequal " , Comparison ( Not_equal , Compare_generic ) ; " % lessequal " , Comparison ( Less_equal , Compare_generic ) ; " % lessthan " , Comparison ( Less_than , Compare_generic ) ; " % greaterequal " , Comparison ( Greater_equal , Compare_generic ) ; " % greaterthan " , Comparison ( Greater_than , Compare_generic ) ; " % compare " , Comparison ( Compare , Compare_generic ) ; ]
let lookup_primitive loc p = match Hashtbl . find primitives_table p . prim_name with | prim -> prim | exception Not_found -> if String . length p . prim_name > 0 && p . prim_name . [ 0 ] = ' ' % then raise ( Error ( loc , Unknown_builtin_primitive p . prim_name ) ) ; External p
let lookup_primitive_and_mark_used loc p env path = match lookup_primitive loc p with | External _ as e -> add_used_primitive loc env path ; e | x -> x
let simplify_constant_constructor = function | Equal -> true | Not_equal -> true | Less_equal -> false | Less_than -> false | Greater_equal -> false | Greater_than -> false | Compare -> false
let glb_array_type t1 t2 = match t1 , t2 with | Pfloatarray , ( Paddrarray | Pintarray ) | ( Paddrarray | Pintarray ) , Pfloatarray -> t1 | Pgenarray , x | x , Pgenarray -> x | Paddrarray , x | x , Paddrarray -> x | Pintarray , Pintarray -> Pintarray | Pfloatarray , Pfloatarray -> Pfloatarray
let specialize_primitive env ty ~ has_constant_constructor prim = let param_tys = match is_function_type env ty with | None -> [ ] | Some ( p1 , rhs ) -> match is_function_type env rhs with | None -> [ p1 ] | Some ( p2 , _ ) -> [ p1 ; p2 ] in match prim , param_tys with | Primitive ( Psetfield ( n , Pointer , init ) , arity ) , [ _ ; p2 ] -> begin match maybe_pointer_type env p2 with | Pointer -> None | Immediate -> Some ( Primitive ( Psetfield ( n , Immediate , init ) , arity ) ) end | Primitive ( Parraylength t , arity ) , [ p ] -> begin let array_type = glb_array_type t ( array_type_kind env p ) in if t = array_type then None else Some ( Primitive ( Parraylength array_type , arity ) ) end | Primitive ( Parrayrefu t , arity ) , p1 :: _ -> begin let array_type = glb_array_type t ( array_type_kind env p1 ) in if t = array_type then None else Some ( Primitive ( Parrayrefu array_type , arity ) ) end | Primitive ( Parraysetu t , arity ) , p1 :: _ -> begin let array_type = glb_array_type t ( array_type_kind env p1 ) in if t = array_type then None else Some ( Primitive ( Parraysetu array_type , arity ) ) end | Primitive ( Parrayrefs t , arity ) , p1 :: _ -> begin let array_type = glb_array_type t ( array_type_kind env p1 ) in if t = array_type then None else Some ( Primitive ( Parrayrefs array_type , arity ) ) end | Primitive ( Parraysets t , arity ) , p1 :: _ -> begin let array_type = glb_array_type t ( array_type_kind env p1 ) in if t = array_type then None else Some ( Primitive ( Parraysets array_type , arity ) ) end | Primitive ( Pbigarrayref ( unsafe , n , Pbigarray_unknown , Pbigarray_unknown_layout ) , arity ) , p1 :: _ -> begin let ( k , l ) = bigarray_type_kind_and_layout env p1 in match k , l with | Pbigarray_unknown , Pbigarray_unknown_layout -> None | _ , _ -> Some ( Primitive ( Pbigarrayref ( unsafe , n , k , l ) , arity ) ) end | Primitive ( Pbigarrayset ( unsafe , n , Pbigarray_unknown , Pbigarray_unknown_layout ) , arity ) , p1 :: _ -> begin let ( k , l ) = bigarray_type_kind_and_layout env p1 in match k , l with | Pbigarray_unknown , Pbigarray_unknown_layout -> None | _ , _ -> Some ( Primitive ( Pbigarrayset ( unsafe , n , k , l ) , arity ) ) end | Primitive ( Pmakeblock ( tag , mut , None ) , arity ) , fields -> begin let shape = List . map ( Typeopt . value_kind env ) fields in let useful = List . exists ( fun knd -> knd <> Pgenval ) shape in if useful then Some ( Primitive ( Pmakeblock ( tag , mut , Some shape ) , arity ) ) else None end | Comparison ( comp , Compare_generic ) , p1 :: _ -> if ( has_constant_constructor && simplify_constant_constructor comp ) then begin Some ( Comparison ( comp , Compare_ints ) ) end else if ( is_base_type env p1 Predef . path_int || is_base_type env p1 Predef . path_char || ( maybe_pointer_type env p1 = Immediate ) ) then begin Some ( Comparison ( comp , Compare_ints ) ) end else if is_base_type env p1 Predef . path_float then begin Some ( Comparison ( comp , Compare_floats ) ) end else if is_base_type env p1 Predef . path_string then begin Some ( Comparison ( comp , Compare_strings ) ) end else if is_base_type env p1 Predef . path_bytes then begin Some ( Comparison ( comp , Compare_bytes ) ) end else if is_base_type env p1 Predef . path_nativeint then begin Some ( Comparison ( comp , Compare_nativeints ) ) end else if is_base_type env p1 Predef . path_int32 then begin Some ( Comparison ( comp , Compare_int32s ) ) end else if is_base_type env p1 Predef . path_int64 then begin Some ( Comparison ( comp , Compare_int64s ) ) end else begin None end | _ -> None
let unboxed_compare name native_repr = Primitive . make ~ name ~ alloc : false ~ native_name ( : name " ^ _unboxed " ) ~ native_repr_args [ : native_repr ; native_repr ] ~ native_repr_res : Untagged_int
let caml_equal = Primitive . simple ~ name " : caml_equal " ~ arity : 2 ~ alloc : true
let caml_string_equal = Primitive . simple ~ name " : caml_string_equal " ~ arity : 2 ~ alloc : false