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 ...
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 , po...
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...
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 -> asse...
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 ( L...
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 . no...
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 i...
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 le...
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 _ = 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 . Se...
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_pat...
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 ...
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 _ -> m...
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_pa...
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 . su...
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 -> nat...
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 s...
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_persis...
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...
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 ] ; ...
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 ( m...
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 ( Lpr...
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_l...
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 ( Pset...
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...
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 :@ % ...
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...
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 , [ i...
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...
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 . ...
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 " , Pr...
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 ) , ...
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