text
stringlengths
12
786k
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_stri...
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_...
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_l...
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 . ...
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 ] , Loc...
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...
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 . ...
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 | U...
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 @ \ ...
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 = ...
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 ,...
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 , ex...
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_...
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 pat...
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 ) ...
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 ) ) ; Extern...
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 ...
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 | Primit...
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
let caml_bytes_equal = Primitive . simple ~ name " : caml_bytes_equal " ~ arity : 2 ~ alloc : false
let caml_notequal = Primitive . simple ~ name " : caml_notequal " ~ arity : 2 ~ alloc : true
let caml_string_notequal = Primitive . simple ~ name " : caml_string_notequal " ~ arity : 2 ~ alloc : false
let caml_bytes_notequal = Primitive . simple ~ name " : caml_bytes_notequal " ~ arity : 2 ~ alloc : false
let caml_lessequal = Primitive . simple ~ name " : caml_lessequal " ~ arity : 2 ~ alloc : true
let caml_string_lessequal = Primitive . simple ~ name " : caml_string_lessequal " ~ arity : 2 ~ alloc : false
let caml_bytes_lessequal = Primitive . simple ~ name " : caml_bytes_lessequal " ~ arity : 2 ~ alloc : false
let caml_lessthan = Primitive . simple ~ name " : caml_lessthan " ~ arity : 2 ~ alloc : true
let caml_string_lessthan = Primitive . simple ~ name " : caml_string_lessthan " ~ arity : 2 ~ alloc : false
let caml_bytes_lessthan = Primitive . simple ~ name " : caml_bytes_lessthan " ~ arity : 2 ~ alloc : false
let caml_greaterequal = Primitive . simple ~ name " : caml_greaterequal " ~ arity : 2 ~ alloc : true
let caml_string_greaterequal = Primitive . simple ~ name " : caml_string_greaterequal " ~ arity : 2 ~ alloc : false
let caml_bytes_greaterequal = Primitive . simple ~ name " : caml_bytes_greaterequal " ~ arity : 2 ~ alloc : false
let caml_greaterthan = Primitive . simple ~ name " : caml_greaterthan " ~ arity : 2 ~ alloc : true
let caml_string_greaterthan = Primitive . simple ~ name " : caml_string_greaterthan " ~ arity : 2 ~ alloc : false
let caml_bytes_greaterthan = Primitive . simple ~ name " : caml_bytes_greaterthan " ~ arity : 2 ~ alloc : false
let caml_compare = Primitive . simple ~ name " : caml_compare " ~ arity : 2 ~ alloc : true
let caml_int_compare = Primitive . simple ~ name " : caml_int_compare " ~ arity : 2 ~ alloc : false
let caml_float_compare = unboxed_compare " caml_float_compare " Unboxed_float
let caml_string_compare = Primitive . simple ~ name " : caml_string_compare " ~ arity : 2 ~ alloc : false
let caml_bytes_compare = Primitive . simple ~ name " : caml_bytes_compare " ~ arity : 2 ~ alloc : false
let caml_nativeint_compare = unboxed_compare " caml_nativeint_compare " ( Unboxed_integer Pnativeint )
let caml_int32_compare = unboxed_compare " caml_int32_compare " ( Unboxed_integer Pint32 )
let caml_int64_compare = unboxed_compare " caml_int64_compare " ( Unboxed_integer Pint64 )
let comparison_primitive comparison comparison_kind = match comparison , comparison_kind with | Equal , Compare_generic -> Pccall caml_equal | Equal , Compare_ints -> Pintcomp Ceq | Equal , Compare_floats -> Pfloatcomp CFeq | Equal , Compare_strings -> Pccall caml_string_equal | Equal , Compare_by...
let lambda_of_loc kind loc = let loc_start = loc . Location . loc_start in let ( file , lnum , cnum ) = Location . get_pos_info loc_start in let file = if Filename . is_relative file then file else Location . rewrite_absolute_path file in let enum = loc . Location . loc_end . Lexing . pos...
let caml_restore_raw_backtrace = Primitive . simple ~ name " : caml_restore_raw_backtrace " ~ arity : 2 ~ alloc : false
let try_ids = Hashtbl . create 8
let add_exception_ident id = Hashtbl . replace try_ids id ( )
let remove_exception_ident id = Hashtbl . remove try_ids id
let lambda_of_prim prim_name prim loc args arg_exps = match prim , args with | Primitive ( prim , arity ) , args when arity = List . length args -> Lprim ( prim , args , loc ) | External prim , args when prim = prim_sys_argv -> Lprim ( Pccall prim , Lconst ( Const_pointer 0 ) :: a...
let check_primitive_arity loc p = let prim = lookup_primitive loc p in let ok = match prim with | Primitive ( _ , arity ) -> arity = p . prim_arity | External _ -> true | Comparison _ -> p . prim_arity = 2 | Raise _ -> p . prim_arity = 1 | Raise_with_backtrace -> p . prim_arity = 2 ...
let transl_primitive loc p env ty path = let prim = lookup_primitive_and_mark_used loc p env path in let has_constant_constructor = false in let prim = match specialize_primitive env ty ~ has_constant_constructor prim with | None -> prim | Some prim -> prim in let rec make_params n = if n <= 0 then [ ] ...
let primitive_is_ccall = function | Pccall _ | Pstringrefs | Pbytesrefs | Pbytessets | Parrayrefs _ | Parraysets _ | Pbigarrayref _ | Pbigarrayset _ | Pduprecord _ | Pdirapply | Prevapply -> true | _ -> false
let primitive_needs_event_after = function | Primitive ( prim , _ ) -> primitive_is_ccall prim | External _ -> true | Comparison ( comp , knd ) -> primitive_is_ccall ( comparison_primitive comp knd ) | Lazy_force | Send | Send_self | Send_cache -> true | Raise _ | Raise_with_backtrace ...
let transl_primitive_application loc p env ty path exp args arg_exps = let prim = lookup_primitive_and_mark_used loc p env ( Some path ) in let has_constant_constructor = match arg_exps with | [ _ ; { exp_desc = Texp_construct ( _ , { cstr_tag = Cstr_constant _ } , _ ) } ] | [ { ...
let report_error ppf = function | Unknown_builtin_primitive prim_name -> fprintf ppf " Unknown builtin primitive " \% s " " \ prim_name | Wrong_arity_builtin_primitive prim_name -> fprintf ppf " Wrong arity for builtin primitive " \% s " " \ prim_name