text
stringlengths
12
786k
let to_clambda_symbol ' env sym : Clambda . uconstant = let lbl = Linkage_name . to_string ( Symbol . label sym ) in Uconst_ref ( lbl , to_uconst_symbol env sym )
let to_clambda_symbol env sym : Clambda . ulambda = Uconst ( to_clambda_symbol ' env sym )
let to_clambda_const env ( const : Flambda . constant_defining_value_block_field ) : Clambda . uconstant = match const with | Symbol symbol -> to_clambda_symbol ' env symbol | Const ( Int i ) -> Uconst_int i | Const ( Char c ) -> Uconst_int ( Char . code c )
let rec to_clambda t env ( flam : Flambda . t ) : Clambda . ulambda = match flam with | Var var -> subst_var env var | Let { var ; defining_expr ; body ; _ } -> let id , env_body = Env . add_fresh_ident env var in Ulet ( Immutable , Pgenval , VP . create id , to_clambda_named t ...
let to_clambda_initialize_symbol t env symbol fields : Clambda . ulambda = let fields = List . map ( fun ( index , expr ) -> index , to_clambda t env expr ) fields in let build_setfield ( index , field ) : Clambda . ulambda = Uprim ( Psetfield ( index , Pointer , Root_initializatio...
let accumulate_structured_constants t env symbol ( c : Flambda . constant_defining_value ) acc = match c with | Allocated_const c -> Symbol . Map . add symbol ( to_clambda_allocated_constant c ) acc | Block ( tag , fields ) -> let fields = List . map ( to_clambda_const env ) fields in S...
let to_clambda_program t env constants ( program : Flambda . program ) = let rec loop env constants ( program : Flambda . program_body ) : Clambda . ulambda * Clambda . ustructured_constant Symbol . Map . t * Clambda . preallocated_block list = match program with | Let_symbol ( symbol , ...
type result = { expr : Clambda . ulambda ; preallocated_blocks : Clambda . preallocated_block list ; structured_constants : Clambda . ustructured_constant Symbol . Map . t ; exported : Export_info . t ; }
let convert ~ ppf_dump ( program , exported_transient ) : result = let current_unit = let closures = Closure_id . Map . keys ( Flambda_utils . make_closure_map program ) in let constant_closures = Flambda_utils . all_lifted_constant_closures program in let offsets = Closure_offsets . compute ...
let map_accum_left f env l = let next ( acc , env ) x = let y , env = f env x in y :: acc , env in let acc , env = List . fold_left next ( [ ] , env ) l in List . rev acc , env
module type Convertible_id = sig type t type fexpr_id include Container_types . S with type t := t val desc : string val name : t -> string val add_tag : string -> int -> string val mk_fexpr_id : string -> fexpr_id end
let default_add_tag name tag = Printf . sprintf " % s_ % d " name tag
module Name_map ( I : Convertible_id ) : sig type t val empty : t val bind : t -> I . t -> I . fexpr_id * t val bind_to : t -> I . t -> I . fexpr_id -> t val find_exn : t -> I . t -> I . fexpr_id module String_map = Map . Make ( String ) type t = { id_map : I . fexpr_id I . ...
module Global_name_map ( I : Convertible_id ) : sig type t val create : unit -> t val translate : t -> I . t -> I . fexpr_id module String_tbl = Hashtbl . Make ( struct include String let hash = Hashtbl . hash end ) type t = { mutable id_tbl : I . fexpr_id I . Map . t ; names : int...
let nowhere a = { Fexpr . txt = a ; loc = Loc_unknown }
module Env : sig type t val create : unit -> t val bind_var : t -> Variable . t -> Fexpr . variable * t val bind_bound_var : t -> Bound_var . t -> Fexpr . variable * t val bind_symbol : t -> Symbol . t -> Fexpr . symbol * t val bind_code_id : t -> Code_id . t -> Fexpr . code_id * t va...
let name env n = Name . pattern_match n ~ var ( : fun v : Fexpr . name -> Var ( Env . find_var_exn env v ) ) ~ symbol ( : fun s : Fexpr . name -> Symbol ( Env . find_symbol_exn env s ) )
let float f = f |> Numeric_types . Float_by_bit_pattern . to_float
let targetint i = i |> Targetint_32_64 . to_int64
let const c : Fexpr . const = match Reg_width_const . descr c with | Naked_immediate imm -> Naked_immediate ( imm |> Targetint_31_63 . to_targetint ' |> Targetint_32_64 . to_string ) | Tagged_immediate imm -> Tagged_immediate ( imm |> Targetint_31_63 . to_targetint ' |> Targetint_32_64 . ...
let depth_or_infinity ( d : int Or_infinity . t ) : Fexpr . rec_info = match d with Finite d -> Depth d | Infinity -> Infinity
let rec rec_info env ( ri : Rec_info_expr . t ) : Fexpr . rec_info = match ri with | Const { depth ; unrolling } -> begin match unrolling with | Not_unrolling -> depth_or_infinity depth | Unrolling { remaining_depth } -> Unroll ( remaining_depth , depth_or_infinity depth ) | Do_not_unr...
let coercion env ( co : Coercion . t ) : Fexpr . coercion = match co with | Id -> Id | Change_depth { from ; to_ } -> let from = rec_info env from in let to_ = rec_info env to_ in Change_depth { from ; to_ }
let simple env s = Simple . pattern_match s ~ name ( : fun n ~ coercion : co : Fexpr . simple -> let s : Fexpr . simple = match name env n with Var v -> Var v | Symbol s -> Symbol s in if Coercion . is_id co then s else let co = coercion env co in Coerce ( s , co ) ) ~ const ( : fun c...
let kind ( k : Flambda_kind . t ) : Fexpr . kind = match k with | Value -> Value | Region -> Region | Naked_number nnk -> Naked_number nnk | Rec_info -> Rec_info
let kind_with_subkind ( k : Flambda_kind . With_subkind . t ) = let rec convert ( k : Flambda_kind . With_subkind . descr ) : Fexpr . kind_with_subkind = match k with | Any_value -> Any_value | Block { tag ; fields } -> let fields = List . map convert fields in Block { tag ; fiel...
let arity ( a : Flambda_arity . With_subkinds . t ) : Fexpr . arity = List . map kind_with_subkind ( Flambda_arity . With_subkinds . to_list a )
let is_default_kind_with_subkind ( k : Flambda_kind . With_subkind . t ) = match Flambda_kind . With_subkind . descr k with | Any_value -> true | Block _ | Float_block _ | Naked_number _ | Boxed_float | Boxed_int32 | Boxed_int64 | Boxed_nativeint | Tagged_immediate | Rec_info | Float_array ...
let kind_with_subkind_opt ( k : Flambda_kind . With_subkind . t ) : Fexpr . kind_with_subkind option = if is_default_kind_with_subkind k then None else Some ( kind_with_subkind k )
let is_default_arity ( a : Flambda_arity . With_subkinds . t ) = match Flambda_arity . With_subkinds . to_list a with | [ k ] -> is_default_kind_with_subkind k | _ -> false
let arity_opt ( a : Flambda_arity . With_subkinds . t ) : Fexpr . arity option = if is_default_arity a then None else Some ( arity a )
let kinded_parameter env ( kp : Bound_parameter . t ) : Fexpr . kinded_parameter * Env . t = let k = Bound_parameter . kind kp |> kind_with_subkind_opt in let param , env = Env . bind_var env ( Bound_parameter . var kp ) in { param ; kind = k } , env
let targetint_ocaml ( i : Targetint_31_63 . Imm . t ) : Fexpr . targetint = i |> Targetint_31_63 . Imm . to_int64
let recursive_flag ( r : Recursive . t ) : Fexpr . is_recursive = match r with Recursive -> Recursive | Non_recursive -> Nonrecursive
let unop env ( op : Flambda_primitive . unary_primitive ) : Fexpr . unop = match op with | Array_length -> Array_length | Box_number ( bk , _alloc_mode ) -> Box_number bk | Tag_immediate -> Tag_immediate | Get_tag -> Get_tag | Is_int -> Is_int | Num_conv { src ; dst } -> Num_conv { ...
let binop ( op : Flambda_primitive . binary_primitive ) : Fexpr . binop = match op with | Array_load ( ak , mut ) -> Array_load ( ak , mut ) | Block_load ( access_kind , mutability ) -> let size ( s : _ Or_unknown . t ) = match s with Known s -> Some ( s |> targetint_ocaml ...
let ternop ( op : Flambda_primitive . ternary_primitive ) : Fexpr . ternop = match op with | Array_set ( ak , ia ) -> Array_set ( ak , ia ) | Block_set _ | Bytes_or_bigstring_set _ | Bigarray_set _ -> Misc . fatal_errorf " TODO : Ternary primitive : % a " Flambda_primitive . W...
let varop ( op : Flambda_primitive . variadic_primitive ) : Fexpr . varop = match op with | Make_block ( Values ( tag , _ ) , mutability , _alloc_mode ) -> Make_block ( tag |> Tag . Scannable . to_int , mutability ) | Make_block ( Naked_floats , _ , _ ) | Make_array _...
let prim env ( p : Flambda_primitive . t ) : Fexpr . prim = match p with | Nullary _ -> Misc . fatal_errorf " TODO : Nullary primitive " | Unary ( op , arg ) -> Unary ( unop env op , simple env arg ) | Binary ( op , arg1 , arg2 ) -> Binary ( binop op , simple env arg1 , ...
let value_slots env map = List . map ( fun ( var , value ) -> let var = Env . translate_value_slot env var in let value = simple env value in { Fexpr . var ; value } ) ( map |> Value_slot . Map . bindings )
let function_declaration env code_id function_slot : Fexpr . fun_decl = let code_id = Env . find_code_id_exn env code_id in let function_slot = Env . translate_function_slot env function_slot in let function_slot = if String . equal code_id . txt function_slot . txt then None else Some function_slot in...
let set_of_closures env sc = let fun_decls = List . map ( fun ( function_slot , fun_decl ) -> function_declaration env fun_decl function_slot ) ( Set_of_closures . function_decls sc |> Function_declarations . funs_in_order |> Function_slot . Lmap . bindings ) in let elts = value_slots env ...
let field_of_block env ( field : Field_of_static_block . t ) : Fexpr . field_of_block = match field with | Symbol symbol -> Symbol ( Env . find_symbol_exn env symbol ) | Tagged_immediate imm -> Tagged_immediate ( imm |> Targetint_31_63 . to_targetint ' |> Targetint_32_64 . to_string ) ...
let or_variable f env ( ov : _ Or_variable . t ) : _ Fexpr . or_variable = match ov with | Const c -> Const ( f c ) | Var ( v , _dbg ) -> Var ( Env . find_var_exn env v )
let static_const env ( sc : Static_const . t ) : Fexpr . static_data = match sc with | Block ( tag , mutability , fields ) -> let tag = tag |> Tag . Scannable . to_int in let elements = List . map ( field_of_block env ) fields in Block { tag ; mutability ; elements } | Set_of_...
let inlining_state ( is : Inlining_state . t ) : Fexpr . inlining_state option = if Inlining_state . equal is ( Inlining_state . default ~ round : 0 ) then None else let depth = Inlining_state . depth is in Some { depth }
let rec expr env e = match Flambda . Expr . descr e with | Let l -> let_expr env l | Let_cont lc -> let_cont_expr env lc | Apply app -> apply_expr env app | Apply_cont app_cont -> apply_cont_expr env app_cont | Switch switch -> switch_expr env switch | Invalid { message } -> invalid_expr env ~ mes...
module Iter = struct let rec expr f_c f_s e = match ( Expr . descr e : Expr . descr ) with | Let e ' -> let_expr f_c f_s e ' | Let_cont e ' -> let_cont f_c f_s e ' | Apply e ' -> apply_expr f_c f_s e ' | Apply_cont e ' -> apply_cont f_c f_s e ' | Switch e ' -> switch f_c f_s e ' ...
let ignore_code ~ id : _ _ = ( )
let ignore_set_of_closures ~ closure_symbols : _ ~ is_phantom : _ _ = ( )
let iter ( ? code = ignore_code ) ( ? set_of_closures = ignore_set_of_closures ) unit = Iter . expr code set_of_closures ( Flambda_unit . body unit )
let bind_all_code_ids env unit = let env = ref env in iter unit ~ code ( : fun ~ id _code -> let _id , new_env = Env . bind_code_id ! env id in env := new_env ) ; ! env
let conv flambda_unit = let done_ = Flambda_unit . return_continuation flambda_unit in let error = Flambda_unit . exn_continuation flambda_unit in let env = Env . create ( ) in let env = Env . bind_special_continuation env done_ ~ to_ : Done in let env = Env . bind_special_continuation env error ...
type t = { return_continuation : Continuation . t ; exn_continuation : Continuation . t ; body : Flambda . Expr . t ; module_symbol : Symbol . t ; used_value_slots : Value_slot . Set . t Or_unknown . t }
let create ~ return_continuation ~ exn_continuation ~ body ~ module_symbol ~ used_value_slots = { return_continuation ; exn_continuation ; body ; module_symbol ; used_value_slots }
let return_continuation t = t . return_continuation
let exn_continuation t = t . exn_continuation
let body t = t . body
let module_symbol t = t . module_symbol
let used_value_slots t = t . used_value_slots
let [ @ ocamlformat " disable " ] print ppf { return_continuation ; exn_continuation ; body ; module_symbol ; used_value_slots ; } = Format . fprintf ppf " [ @< hov 1 ( >\ [ @< hov 1 ( > module_symbol @ % a ) ] @@ \ [ @< hov 1 ( > return_continuation @ % a ) ...
let name_expr ~ name ( named : Flambda . named ) : Flambda . t = let var = Variable . create ~ current_compilation_unit ( : Compilation_unit . get_current_exn ( ) ) name in Flambda . create_let var named ( Var var )
let name_expr_from_var ~ var ( named : Flambda . named ) : Flambda . t = let var = Variable . rename ~ current_compilation_unit ( : Compilation_unit . get_current_exn ( ) ) var in Flambda . create_let var named ( Var var )
let find_declaration cf ( { funs } : Flambda . function_declarations ) = Variable . Map . find ( Closure_id . unwrap cf ) funs
let find_declaration_variable cf ( { funs } : Flambda . function_declarations ) = let var = Closure_id . unwrap cf in if not ( Variable . Map . mem var funs ) then raise Not_found else var
let find_free_variable cv ( { free_vars } : Flambda . set_of_closures ) = let var : Flambda . specialised_to = Variable . Map . find ( Var_within_closure . unwrap cv ) free_vars in var . var
let function_arity ( f : Flambda . function_declaration ) = List . length f . params
let variables_bound_by_the_closure cf ( decls : Flambda . function_declarations ) = let func = find_declaration cf decls in let params = Parameter . Set . vars func . params in let functions = Variable . Map . keys decls . funs in Variable . Set . diff ( Variable . Set . diff func . f...
let description_of_toplevel_node ( expr : Flambda . t ) = match expr with | Var id -> Format . asprintf " var % a " Variable . print id | Apply _ -> " apply " | Assign _ -> " assign " | Send _ -> " send " | Proved_unreachable -> " unreachable " | Let { var ; _ } ->...
let equal_direction_flag ( x : Asttypes . direction_flag ) ( y : Asttypes . direction_flag ) = match x , y with | Upto , Upto -> true | Downto , Downto -> true | ( Upto | Downto ) , _ -> false
let rec same ( l1 : Flambda . t ) ( l2 : Flambda . t ) = l1 == l2 || match ( l1 , l2 ) with | Var v1 , Var v2 -> Variable . equal v1 v2 | Var _ , _ | _ , Var _ -> false | Apply a1 , Apply a2 -> Flambda . equal_call_kind a1 . kind a2 . kind && Variable . equal a1 . ...
let toplevel_substitution sb tree = let sb ' = sb in let sb v = try Variable . Map . find v sb with Not_found -> v in let aux ( flam : Flambda . t ) : Flambda . t = match flam with | Var var -> let var = sb var in Var var | Let_mutable mutable_let -> let initial_value = sb mutable_let . ini...
let toplevel_substitution_named sb named = let name = Internal_variable_names . toplevel_substitution_named in let expr = name_expr named ~ name in match toplevel_substitution sb expr with | Let let_expr -> let_expr . defining_expr | _ -> assert false
let make_closure_declaration ~ is_classic_mode ~ id ~ alloc_mode ~ region ~ body ~ params ~ stub : Flambda . t = let free_variables = Flambda . free_variables body in let param_set = Parameter . Set . vars params in if not ( Variable . Set . subset param_set free_variables ) then begin Misc ....
let bind ~ bindings ~ body = List . fold_left ( fun expr ( var , var_def ) -> Flambda . create_let var var_def expr ) body bindings
let all_lifted_constants ( program : Flambda . program ) = let rec loop ( program : Flambda . program_body ) = match program with | Let_symbol ( symbol , decl , program ) -> ( symbol , decl ) :: ( loop program ) | Let_rec_symbol ( decls , program ) -> List . fold_left ( ...
let all_lifted_constants_as_map program = Symbol . Map . of_list ( all_lifted_constants program )
let initialize_symbols ( program : Flambda . program ) = let rec loop ( program : Flambda . program_body ) = match program with | Initialize_symbol ( symbol , tag , fields , program ) -> ( symbol , tag , fields ) :: ( loop program ) | Effect ( _ , program ) | Let_symbol...
let imported_symbols ( program : Flambda . program ) = program . imported_symbols
let needed_import_symbols ( program : Flambda . program ) = let dependencies = Flambda . free_symbols_program program in let defined_symbol = Symbol . Set . union ( Symbol . Set . of_list ( List . map fst ( all_lifted_constants program ) ) ) ( Symbol . Set . of_list ( List . ...
let introduce_needed_import_symbols program : Flambda . program = { program with imported_symbols = needed_import_symbols program ; }
let root_symbol ( program : Flambda . program ) = let rec loop ( program : Flambda . program_body ) = match program with | Effect ( _ , program ) | Let_symbol ( _ , _ , program ) | Let_rec_symbol ( _ , program ) | Initialize_symbol ( _ , _ , _ , program ) -> loo...
let might_raise_static_exn flam stexn = try Flambda_iterators . iter_on_named ( function | Flambda . Static_raise ( ex , _ ) when Static_exception . equal ex stexn -> raise Exit | _ -> ( ) ) ( fun _ -> ( ) ) flam ; false with Exit -> true
let make_closure_map program = let map = ref Closure_id . Map . empty in let add_set_of_closures ~ constant : _ : Flambda . set_of_closures -> unit = fun { function_decls } -> Variable . Map . iter ( fun var _ -> let closure_id = Closure_id . wrap var in let set_of_closures_id = function_...
let all_lifted_constant_closures program = List . fold_left ( fun unchanged flambda -> match flambda with | ( _ , Flambda . Set_of_closures { function_decls = { funs } } ) -> Variable . Map . fold ( fun key ( _ : Flambda . function_declaration ) acc -> Closure_id . Set . add...
let all_lifted_constant_sets_of_closures program = let set = ref Set_of_closures_id . Set . empty in List . iter ( function | ( _ , Flambda . Set_of_closures { function_decls = { set_of_closures_id } } ) -> set := Set_of_closures_id . Set . add set_of_closures_id ! set | _ -> ( ...
let all_sets_of_closures program = let list = ref [ ] in Flambda_iterators . iter_on_set_of_closures_of_program program ~ f ( : fun ~ constant : _ set_of_closures -> list := set_of_closures :: ! list ) ; ! list
let all_sets_of_closures_map program = let r = ref Set_of_closures_id . Map . empty in Flambda_iterators . iter_on_set_of_closures_of_program program ~ f ( : fun ~ constant : _ set_of_closures -> r := Set_of_closures_id . Map . add set_of_closures . function_decls . set_of_closures_id set_of_cl...
let substitute_read_symbol_field_for_variables ( substitution : ( Symbol . t * int list ) Variable . Map . t ) ( expr : Flambda . t ) = let bind var fresh_var ( expr : Flambda . t ) : Flambda . t = let symbol , path = Variable . Map . find var substitution in let rec make_named...
module Switch_storer = Switch . Store ( struct type t = Flambda . t type key = | Var of Variable . t | Let of Variable . t * key_named * key | Static_raise of Static_exception . t * Variable . t list and key_named = | Symbol of Symbol . t | Const of Flambda . const | Prim of Clambda_primi...
let fun_vars_referenced_in_decls ( function_decls : Flambda . function_declarations ) ~ closure_symbol = let fun_vars = Variable . Map . keys function_decls . funs in let symbols_to_fun_vars = Variable . Set . fold ( fun fun_var symbols_to_fun_vars -> let closure_id = Closure_id . wrap fun_va...
let closures_required_by_entry_point ( ~ entry_point : Closure_id . t ) ~ closure_symbol ( function_decls : Flambda . function_declarations ) = let dependencies = fun_vars_referenced_in_decls function_decls ~ closure_symbol in let set = ref Variable . Set . empty in let queue = Queue . create...
let all_functions_parameters ( function_decls : Flambda . function_declarations ) = Variable . Map . fold ( fun _ ( { params } : Flambda . function_declaration ) set -> Variable . Set . union set ( Parameter . Set . vars params ) ) function_decls . funs Variable . Set . em...
let all_free_symbols ( function_decls : Flambda . function_declarations ) = Variable . Map . fold ( fun _ ( function_decl : Flambda . function_declaration ) syms -> Symbol . Set . union syms function_decl . free_symbols ) function_decls . funs Symbol . Set . empty
let contains_stub ( fun_decls : Flambda . function_declarations ) = let number_of_stub_functions = Variable . Map . cardinal ( Variable . Map . filter ( fun _ { Flambda . stub } -> stub ) fun_decls . funs ) in number_of_stub_functions > 0
let clean_projections ~ which_variables = Variable . Map . map ( fun ( spec_to : Flambda . specialised_to ) -> match spec_to . projection with | None -> spec_to | Some projection -> let from = Projection . projecting_from projection in if Variable . Map . mem from which_variables then spec_...
let projection_to_named ( projection : Projection . t ) : Flambda . named = match projection with | Project_var project_var -> Project_var project_var | Project_closure project_closure -> Project_closure project_closure | Move_within_set_of_closures move -> Move_within_set_of_closures move | Field ( ...
type specialised_to_same_as = | Not_specialised | Specialised_and_aliased_to of Variable . Set . t
let parameters_specialised_to_the_same_variable ( ~ function_decls : Flambda . function_declarations ) ( ~ specialised_args : Flambda . specialised_to Variable . Map . t ) = let specialised_arg_aliasing = Variable . Map . transpose_keys_and_data_set ( Variable . Map . map ( fun ( { ...
module StrTbl = Hashtbl . Make ( struct type t = string let equal = String . equal let hash = Hashtbl . hash end )