text
stringlengths
0
601k
let make_const_int64 n = make_const_ref ( Uconst_int64 n )
let simplif_arith_prim_pure ~ backend fpc p ( args , approxs ) dbg = let module B = ( val backend : Backend_intf . S ) in let open Clambda_primitives in let default = ( Uprim ( p , args , dbg ) , Value_unknown ) in match approxs with | [ Value_const ( Uconst_int n1 ) ] -> begin match p with | Pnot -> make_const_bool ( ...
let field_approx n = function | Value_tuple ( _ , a ) when n < Array . length a -> a . ( n ) | Value_const ( Uconst_ref ( _ , Some ( Uconst_block ( _ , l ) ) ) ) when n < List . length l -> Value_const ( List . nth l n ) | _ -> Value_unknown
let simplif_prim_pure ~ backend fpc p ( args , approxs ) dbg = let open Clambda_primitives in match p , args , approxs with | Pmakeblock ( tag , Immutable , _kind , mode ) , _ , _ -> let field = function | Value_const c -> c | _ -> raise Exit in begin try let cst = Uconst_block ( tag , List . map field approxs ) in let...
let simplif_prim ~ backend fpc p ( args , approxs as args_approxs ) dbg = if List . for_all is_pure args then simplif_prim_pure ~ backend fpc p args_approxs dbg else let approx = match p with | P . Pmakeblock ( _ , Immutable , _kind , mode ) -> Value_tuple ( mode , Array . of_list approxs ) | _ -> Value_unknown in ( Up...
let approx_ulam = function Uconst c -> Value_const c | _ -> Value_unknown
let find_action idxs acts tag = if 0 <= tag && tag < Array . length idxs then begin let idx = idxs . ( tag ) in assert ( 0 <= idx && idx < Array . length acts ) ; Some acts . ( idx ) end else None
let subst_debuginfo loc dbg = if ! Clflags . debug then Debuginfo . inline loc dbg else dbg
let rec substitute loc ( ( backend , fpc ) as st ) sb rn ulam = match ulam with Uvar v -> begin try V . Map . find v sb with Not_found -> ulam end | Uconst _ -> ulam | Udirect_apply ( lbl , args , probe , kind , dbg ) -> let dbg = subst_debuginfo loc dbg in Udirect_apply ( lbl , List . map ( substitute loc st sb rn ) a...
type env = { backend : ( module Backend_intf . S ) ; cenv : ulambda V . Map . t ; fenv : value_approximation V . Map . t ; mutable_vars : V . Set . t ; }
let rec is_substituable ~ mutable_vars = function | Uvar v -> not ( V . Set . mem v mutable_vars ) | Uconst _ -> true | Uoffset ( arg , _ ) -> is_substituable ~ mutable_vars arg | _ -> false
let is_erasable = function | Uclosure _ -> true | u -> is_pure u
let bind_params { backend ; mutable_vars ; _ } loc fdesc params args funct body = let fpc = fdesc . fun_float_const_prop in let rec aux subst pl al body = match ( pl , al ) with ( [ ] , [ ] ) -> substitute ( Debuginfo . from_location loc ) ( backend , fpc ) subst ( Some Int . Map . empty ) body | ( p1 :: pl , a1 :: al ...
let warning_if_forced_inlined ~ loc ~ attribute warning = if attribute = Always_inlined then Location . prerr_warning ( Debuginfo . Scoped_location . to_location loc ) ( Warnings . Inlining_impossible warning )
let fail_if_probe ~ probe msg = match probe with | None -> ( ) | Some { name } -> Misc . fatal_errorf " Closure probe % s handler : % s " name msg
let direct_apply env fundesc ufunct uargs pos mode ~ probe ~ loc ~ attribute = match fundesc . fun_inline , attribute with | _ , Never_inlined | None , _ -> let dbg = Debuginfo . from_location loc in let kind = ( pos , mode ) in warning_if_forced_inlined ~ loc ~ attribute " Function information unavailable " ; if not f...
let strengthen_approx appl approx = match approx_ulam appl with ( Value_const _ ) as intapprox -> intapprox | _ -> approx
let check_constant_result ulam approx = match approx with Value_const c when is_pure ulam -> make_const c | Value_global_field ( id , i ) when is_pure ulam -> begin match ulam with | Uprim ( P . Pfield _ , [ Uprim ( P . Pread_symbol _ , _ , _ ) ] , _ ) -> ( ulam , approx ) | _ -> let glb = Uprim ( P . Pread_symbol id ,...
let sequence_constant_expr ulam1 ( ulam2 , approx2 as res2 ) = if is_pure ulam1 then res2 else ( Usequence ( ulam1 , ulam2 ) , approx2 )
let global_approx = ref ( [ ] || : value_approximation array )
let function_nesting_depth = ref 0
let close_approx_var { fenv ; cenv } id = let approx = try V . Map . find id fenv with Not_found -> Value_unknown in match approx with Value_const c -> make_const c | approx -> let subst = try V . Map . find id cenv with Not_found -> Uvar id in ( subst , approx )
let close_var env id = let ( ulam , _app ) = close_approx_var env id in ulam
let rec close ( { backend ; fenv ; cenv ; mutable_vars } as env ) lam = let module B = ( val backend : Backend_intf . S ) in match lam with | Lvar id -> close_approx_var env id | Lconst cst -> let str ( ? shared = true ) cst = let name = Compilenv . new_structured_constant cst ~ shared in Uconst_ref ( name , Some cst )...
let collect_exported_structured_constants a = let rec approx = function | Value_closure ( _ , fd , a ) -> approx a ; begin match fd . fun_inline with | Some ( _ , u ) -> ulam u | None -> ( ) end | Value_tuple ( _ , a ) -> Array . iter approx a | Value_const c -> const c | Value_unknown | Value_global_field _ -> ( ) and...
let reset ( ) = global_approx := [ ] ; || function_nesting_depth := 0
let intro ~ backend ~ size lam = reset ( ) ; let id = Compilenv . make_symbol None in global_approx := Array . init size ( fun i -> Value_global_field ( id , i ) ) ; Compilenv . set_global_approx ( Value_tuple ( alloc_heap , ! global_approx ) ) ; let ( ulam , _approx ) = close { backend ; fenv = V . Map . empty ; cenv ...
type t = { current_unit_id : Ident . t ; symbol_for_global ' : ( Ident . t -> Symbol . t ) ; filename : string ; backend : ( module Backend_intf . S ) ; mutable imported_symbols : Symbol . Set . t ; mutable declared_symbols : ( Symbol . t * Flambda . constant_defining_value ) list ; }
let add_default_argument_wrappers lam = let defs_are_all_functions ( defs : ( _ * Lambda . lambda ) list ) = List . for_all ( function ( _ , Lambda . Lfunction _ ) -> true | _ -> false ) defs in let f ( lam : Lambda . lambda ) : Lambda . lambda = match lam with | Llet ( ( Strict | Alias | StrictOpt ) , _k , id , Lfunct...
let tupled_function_call_stub original_params unboxed_version ~ closure_bound_var ~ region : Flambda . function_declaration = let tuple_param_var = Variable . rename unboxed_version in let params = List . map ( fun p -> Variable . rename p ) original_params in let call : Flambda . t = Apply ( { func = unboxed_version ;...
let register_const t ( constant : Flambda . constant_defining_value ) name : Flambda . constant_defining_value_block_field * Internal_variable_names . t = let var = Variable . create name in let symbol = Symbol . of_variable var in t . declared_symbols <- ( symbol , constant ) :: t . declared_symbols ; Symbol symbol , ...
let rec declare_const t ( const : Lambda . structured_constant ) : Flambda . constant_defining_value_block_field * Internal_variable_names . t = match const with | Const_base ( Const_int c ) -> ( Const ( Int c ) , Names . const_int ) | Const_base ( Const_char c ) -> ( Const ( Char c ) , Names . const_char ) | Const_bas...
let close_const t ( const : Lambda . structured_constant ) : Flambda . named * Internal_variable_names . t = match declare_const t const with | Const c , name -> Const c , name | Symbol s , name -> Symbol s , name
let lambda_const_bool b : Lambda . structured_constant = if b then Lambda . const_int 1 else Lambda . const_int 0
let lambda_const_int i : Lambda . structured_constant = Const_base ( Const_int i )
let rec close t env ( lam : Lambda . lambda ) : Flambda . t = match lam with | Lvar id -> begin match Env . find_var_exn env id with | var -> Var var | exception Not_found -> match Env . find_mutable_var_exn env id with | mut_var -> name_expr ( Read_mutable mut_var ) ~ name : Names . read_mutable | exception Not_found ...
let lambda_to_flambda ~ backend ~ module_ident ~ size ~ filename lam : Flambda . program = let lam = add_default_argument_wrappers lam in let module Backend = ( val backend : Backend_intf . S ) in let compilation_unit = Compilation_unit . get_current_exn ( ) in let t = { current_unit_id = Compilation_unit . get_persist...
module Env = struct type t = { variables : Variable . t Ident . tbl ; mutable_variables : Mutable_variable . t Ident . tbl ; static_exceptions : Static_exception . t Numbers . Int . Map . t ; globals : Symbol . t Numbers . Int . Map . t ; at_toplevel : bool ; } let empty = { variables = Ident . empty ; mutable_variable...
module Function_decls = struct module Function_decl = struct type t = { let_rec_ident : Ident . t ; closure_bound_var : Variable . t ; kind : Lambda . function_kind ; mode : Lambda . alloc_mode ; region : bool ; params : Ident . t list ; body : Lambda . lambda ; free_idents_of_body : Ident . Set . t ; attr : Lambda . f...
type t = | Not_in_a_closure | In_a_set_of_closures_but_not_yet_in_a_specific_closure | Closure of { code_id : Code_id . t ; return_continuation : Continuation . t ; exn_continuation : Continuation . t }
let [ @ ocamlformat " disable " ] print ppf = function | Not_in_a_closure -> Format . fprintf ppf " not_in_a_closure " | In_a_set_of_closures_but_not_yet_in_a_specific_closure -> Format . fprintf ppf " in_a_set_of_closures " | Closure { code_id ; return_continuation ; exn_continuation ; } -> Format . fprintf ppf " [ @<...
let in_a_closure code_id ~ return_continuation ~ exn_continuation = Closure { code_id ; return_continuation ; exn_continuation }
type in_or_out_of_closure = | In_a_closure | Not_in_a_closure
let in_or_out_of_closure ( t : t ) : in_or_out_of_closure = match t with | Not_in_a_closure -> Not_in_a_closure | Closure _ -> In_a_closure | In_a_set_of_closures_but_not_yet_in_a_specific_closure -> Misc . fatal_errorf " Expected to be either inside or outside a closure , but not \ [ In_a_set_of_closures_but_not_yet_i...
let raw_clambda_dump_if ppf ( ( ulambda , _ , structured_constants ) : Clambda . with_constants ) = if ! Clflags . dump_rawclambda || ! Clflags . dump_clambda then begin Format . fprintf ppf " . @ clambda . " ; :@ Printclambda . clambda ppf ulambda ; List . iter ( fun { Clambda . symbol ; definition ; _ } -> Format . f...
let lambda_to_clambda ~ backend ~ filename : _ ~ prefixname : _ ~ ppf_dump ( lambda : Lambda . program ) = let clambda = Closure . intro ~ backend ~ size : lambda . main_module_block_size lambda . code in let provenance : Clambda . usymbol_provenance = { original_idents = [ ] ; module_path = Path . Pident ( Ident . cre...
type result = { function_offsets : int Closure_id . Map . t ; free_variable_offsets : int Var_within_closure . Map . t ; }
let add_closure_offsets { function_offsets ; free_variable_offsets } ( { function_decls ; free_vars } : Flambda . set_of_closures ) = let assign_function_offset id function_decl ( map , env_pos ) = let pos = env_pos + 1 in let env_pos = let arity = Flambda_utils . function_arity function_decl in env_pos + 1 + 1 + 1 + (...
let compute ( program : Flambda . program ) = let init : result = { function_offsets = Closure_id . Map . empty ; free_variable_offsets = Var_within_closure . Map . empty ; } in let r = List . fold_left add_closure_offsets init ( Flambda_utils . all_sets_of_closures program ) in r
type pers_flags = | Rectypes | Alerts of alerts | Opaque | Unsafe_string
type error = | Not_an_interface of filepath | Wrong_version_interface of filepath * string | Corrupted_interface of filepath
type signature = Types . signature_item list
type flags = pers_flags list
type header = modname * signature
type cmi_infos = { cmi_name : modname ; cmi_sign : signature ; cmi_crcs : crcs ; cmi_flags : flags ; }
let input_cmi ic = let ( name , sign ) = ( input_value ic : header ) in let crcs = ( input_value ic : crcs ) in let flags = ( input_value ic : flags ) in { cmi_name = name ; cmi_sign = sign ; cmi_crcs = crcs ; cmi_flags = flags ; }
let read_cmi filename = let ic = open_in_bin filename in try let buffer = really_input_string ic ( String . length Config . cmi_magic_number ) in if buffer <> Config . cmi_magic_number then begin close_in ic ; let pre_len = String . length Config . cmi_magic_number - 3 in if String . sub buffer 0 pre_len = String . sub...
let output_cmi filename oc cmi = output_string oc Config . cmi_magic_number ; output_value oc ( ( cmi . cmi_name , cmi . cmi_sign ) : header ) ; flush oc ; let crc = Digest . file filename in let crcs = ( cmi . cmi_name , Some crc ) :: cmi . cmi_crcs in output_value oc ( crcs : crcs ) ; output_value oc ( cmi . cmi_flag...
let report_error ppf = function | Not_an_interface filename -> fprintf ppf " % a @ is not a compiled interface " Location . print_filename filename | Wrong_version_interface ( filename , older_newer ) -> fprintf ppf " % a @ is not a compiled interface for this version of OCaml . . @\ It seems to be for % s version of O...
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
type machtype_component = | Val | Addr | Int | Float
type machtype = machtype_component array
let typ_void = ( [ ] || : machtype_component array )
let lub_component comp1 comp2 = match comp1 , comp2 with | Int , Int -> Int | Int , Val -> Val | Int , Addr -> Addr | Val , Int -> Val | Val , Val -> Val | Val , Addr -> Addr | Addr , Int -> Addr | Addr , Addr -> Addr | Addr , Val -> Addr | Float , Float -> Float | ( Int | Addr | Val ) , Float | Float , ( Int | Addr | ...
let ge_component comp1 comp2 = match comp1 , comp2 with | Int , Int -> true | Int , Addr -> false | Int , Val -> false | Val , Int -> true | Val , Val -> true | Val , Addr -> false | Addr , Int -> true | Addr , Addr -> true | Addr , Val -> true | Float , Float -> true | ( Int | Addr | Val ) , Float | Float , ( Int | Ad...
type exttype = | XInt | XInt32 | XInt64 | XFloat
let machtype_of_exttype = function | XInt -> typ_int | XInt32 -> typ_int | XInt64 -> if Arch . size_int = 4 then [ | Int ; Int ] | else typ_int | XFloat -> typ_float
let machtype_of_exttype_list xtl = Array . concat ( List . map machtype_of_exttype xtl )
type integer_comparison = Lambda . integer_comparison = | Ceq | Cne | Clt | Cgt | Cle | Cge
type float_comparison = Lambda . float_comparison = | CFeq | CFneq | CFlt | CFnlt | CFgt | CFngt | CFle | CFnle | CFge | CFnge
let label_counter = ref init_label
let set_label l = if ( l < ! label_counter ) then begin Misc . fatal_errorf " Cannot set label counter to % d , it must be >= % d " l ! label_counter ( ) end ; label_counter := l
let cur_label ( ) = ! label_counter
let new_label ( ) = incr label_counter ; ! label_counter
type rec_flag = Nonrecursive | Recursive
type initialization_or_assignment = | Initialization | Assignment
type phantom_defining_expr = | Cphantom_const_int of Targetint . t | Cphantom_const_symbol of string | Cphantom_var of Backend_var . t | Cphantom_offset_var of { var : Backend_var . t ; offset_in_words : int ; } | Cphantom_read_field of { var : Backend_var . t ; field : int ; } | Cphantom_read_symbol_field of { sym : s...
type memory_chunk = Byte_unsigned | Byte_signed | Sixteen_unsigned | Sixteen_signed | Thirtytwo_unsigned | Thirtytwo_signed | Word_int | Word_val | Single | Double Capply of machtype * Lambda . region_close | Cextcall of string * machtype * exttype list * bool | Cload of memory_chunk * Asttypes . mutable_flag | Calloc ...
type expression = Cconst_int of int * Debuginfo . t | Cconst_natint of nativeint * Debuginfo . t | Cconst_float of float * Debuginfo . t | Cconst_symbol of string * Debuginfo . t | Cvar of Backend_var . t | Clet of Backend_var . With_provenance . t * expression * expression | Clet_mut of Backend_var . With_provenance ....
type codegen_option = | Reduce_code_size | No_CSE
type fundecl = { fun_name : string ; fun_args : ( Backend_var . With_provenance . t * machtype ) list ; fun_body : expression ; fun_codegen_options : codegen_option list ; fun_dbg : Debuginfo . t ; }
type data_item = Cdefine_symbol of string | Cglobal_symbol of string | Cint8 of int | Cint16 of int | Cint32 of nativeint | Cint of nativeint | Csingle of float | Cdouble of float | Csymbol_address of string | Cstring of string | Cskip of int | Calign of int
type phrase = Cfunction of fundecl | Cdata of data_item list
let ccatch ( i , ids , e1 , e2 , dbg ) = Ccatch ( Nonrecursive , [ i , ids , e2 , dbg ] , e1 )
let reset ( ) = label_counter := init_label
let iter_shallow_tail f = function | Clet ( _ , _ , body ) | Cphantom_let ( _ , _ , body ) | Clet_mut ( _ , _ , _ , body ) -> f body ; true | Cifthenelse ( _cond , _ifso_dbg , ifso , _ifnot_dbg , ifnot , _dbg ) -> f ifso ; f ifnot ; true | Csequence ( _e1 , e2 ) -> f e2 ; true | Cswitch ( _e , _tbl , el , _dbg ' ) -> A...
let map_shallow_tail f = function | Clet ( id , exp , body ) -> Clet ( id , exp , f body ) | Clet_mut ( id , kind , exp , body ) -> Clet_mut ( id , kind , exp , f body ) | Cphantom_let ( id , exp , body ) -> Cphantom_let ( id , exp , f body ) | Cifthenelse ( cond , ifso_dbg , ifso , ifnot_dbg , ifnot , dbg ) -> Cifthen...
let map_tail f = let rec loop = function | Cconst_int _ | Cconst_natint _ | Cconst_float _ | Cconst_symbol _ | Cvar _ | Cassign _ | Ctuple _ | Cop _ as c -> f c | cmm -> map_shallow_tail loop cmm in loop
let iter_shallow f = function | Clet ( _id , e1 , e2 ) -> f e1 ; f e2 | Clet_mut ( _id , _kind , e1 , e2 ) -> f e1 ; f e2 | Cphantom_let ( _id , _de , e ) -> f e | Cassign ( _id , e ) -> f e | Ctuple el -> List . iter f el | Cop ( _op , el , _dbg ) -> List . iter f el | Csequence ( e1 , e2 ) -> f e1 ; f e2 | Cifthenels...
let map_shallow f = function | Clet ( id , e1 , e2 ) -> Clet ( id , f e1 , f e2 ) | Clet_mut ( id , kind , e1 , e2 ) -> Clet_mut ( id , kind , f e1 , f e2 ) | Cphantom_let ( id , de , e ) -> Cphantom_let ( id , de , f e ) | Cassign ( id , e ) -> Cassign ( id , f e ) | Ctuple el -> Ctuple ( List . map f el ) | Cop ( op ...
type boxed_number = | Boxed_float of alloc_mode * Debuginfo . t | Boxed_integer of boxed_integer * alloc_mode * Debuginfo . t
type env = { unboxed_ids : ( V . t * boxed_number ) V . tbl ; notify_catch : ( Cmm . expression list -> unit ) IntMap . t ; environment_param : V . t option ; }
let empty_env = { unboxed_ids = V . empty ; notify_catch = IntMap . empty ; environment_param = None ; }
let create_env ~ environment_param = { empty_env with environment_param ; }
let is_unboxed_id id env = try Some ( V . find_same id env . unboxed_ids ) with Not_found -> None
let add_unboxed_id id unboxed_id bn env = { env with unboxed_ids = V . add id ( unboxed_id , bn ) env . unboxed_ids ; }
let add_notify_catch n f env = { env with notify_catch = IntMap . add n f env . notify_catch }
let notify_catch i env l = match IntMap . find_opt i env . notify_catch with | Some f -> f l | None -> ( )
type then_else = | Then_true_else_false | Then_false_else_true | Unknown
let invert_then_else = function | Then_true_else_false -> Then_false_else_true | Then_false_else_true -> Then_true_else_false | Unknown -> Unknown