text
stringlengths
12
786k
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 ) ...
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_va...
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 ( ...
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 ( {...
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 . dec...
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 ( C...
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...
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 = Co...
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 = ...
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...
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 ;...
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...
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 { C...
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 = [ ] ; mo...
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 functi...
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...
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 :...
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 seem...
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 |...
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 | ( I...
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 ; } | Cpha...
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 . m...
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 Back...
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 ( _...
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 ( ...
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 ) -> L...
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...
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
let mut_from_env env ptr = match env . environment_param with | None -> Asttypes . Mutable | Some environment_param -> match ptr with | Cvar ptr -> if V . same environment_param ptr then Asttypes . Immutable else Asttypes . Mutable | _ -> Asttypes . Mutable
let get_field env ptr n dbg = let mut = mut_from_env env ptr in get_field_gen mut ptr n dbg
type rhs_kind = | RHS_block of int | RHS_infix of { blocksize : int ; offset : int } | RHS_floatblock of int | RHS_nonrec ; ;
let rec expr_size env = function | Uvar id -> begin try V . find_same id env with Not_found -> RHS_nonrec end | Uclosure ( fundecls , clos_vars ) -> RHS_block ( fundecls_size fundecls + List . length clos_vars ) | Ulet ( _str , _kind , id , exp , body ) -> expr_size ( V . add ( ...
let transl_constant dbg = function | Uconst_int n -> int_const dbg n | Uconst_ref ( label , def_opt ) -> Option . iter ( fun def -> Cmmgen_state . add_structured_constant label def ) def_opt ; Cconst_symbol ( label , dbg )
let emit_constant cst cont = match cst with | Uconst_int n -> cint_const n :: cont | Uconst_ref ( sym , _ ) -> Csymbol_address sym :: cont
let emit_structured_constant ( ( _sym , is_global ) as symb ) cst cont = match cst with | Uconst_float s -> emit_float_constant symb s cont | Uconst_string s -> emit_string_constant symb s cont | Uconst_int32 n -> emit_int32_constant symb n cont | Uconst_int64 n -> emit_int64_constant symb n cont |...
let box_int_constant sym bi n = match bi with Pnativeint -> emit_nativeint_constant ( sym , Local ) n [ ] | Pint32 -> let n = Nativeint . to_int32 n in emit_int32_constant ( sym , Local ) n [ ] | Pint64 -> let n = Int64 . of_nativeint n in emit_int64_constant ( sym , Local ) n [ ...
let box_int dbg bi mode arg = match arg with | Cconst_int ( n , _ ) -> let sym = Compilenv . new_const_symbol ( ) in let data_items = box_int_constant sym bi ( Nativeint . of_int n ) in Cmmgen_state . add_data_items data_items ; Cconst_symbol ( sym , dbg ) | Cconst_natint ( n , _ ...
let typ_of_boxed_number = function | Boxed_float _ -> Cmm . typ_float | Boxed_integer ( Pint64 , _ , _ ) when size_int = 4 -> [ | Int ; Int ] | | Boxed_integer _ -> Cmm . typ_int
let equal_unboxed_integer ui1 ui2 = match ui1 , ui2 with | Pnativeint , Pnativeint -> true | Pint32 , Pint32 -> true | Pint64 , Pint64 -> true | _ , _ -> false
let equal_boxed_number bn1 bn2 = match bn1 , bn2 with | Boxed_float _ , Boxed_float _ -> true | Boxed_integer ( ui1 , m , _ ) , Boxed_integer ( ui2 , m ' , _ ) -> equal_unboxed_integer ui1 ui2 && Lambda . eq_mode m m ' | _ , _ -> false
let box_number bn arg = match bn with | Boxed_float ( m , dbg ) -> box_float dbg m arg | Boxed_integer ( bi , m , dbg ) -> box_int dbg bi m arg
let unbox_number dbg bn arg = match bn with | Boxed_float ( _ , dbg ) -> unbox_float dbg arg | Boxed_integer ( Pint32 , _ , _ ) -> low_32 dbg ( unbox_int dbg Pint32 arg ) | Boxed_integer ( bi , _ , _ ) -> unbox_int dbg bi arg
type unboxed_number_kind = No_unboxing | Boxed of boxed_number * bool | No_result
let join_unboxed_number_kind ~ strict k1 k2 = match k1 , k2 with | Boxed ( b1 , c1 ) , Boxed ( b2 , c2 ) when equal_boxed_number b1 b2 -> Boxed ( b1 , c1 && c2 ) | No_result , k | k , No_result -> k | No_unboxing , k | k , No_unboxing when not strict -> k | _ , _ -> No_unb...
let is_unboxed_number_cmm ~ strict cmm = let r = ref No_result in let notify k = r := join_unboxed_number_kind ~ strict ! r k in let rec aux = function | Cop ( Calloc mode , [ Cconst_natint ( hdr , _ ) ; _ ] , dbg ) when Nativeint . equal hdr float_header -> notify ( Boxed ( Boxed_...
let rec transl env e = match e with Uvar id -> begin match is_unboxed_id id env with | None -> Cvar id | Some ( unboxed_id , bn ) -> box_number bn ( Cvar unboxed_id ) end | Uconst sc -> transl_constant Debuginfo . none sc | Uclosure ( fundecls , [ ] ) -> let sym = Compilenv . new_cons...
let transl_function f = let body = f . body in let cmm_body = let env = create_env ~ environment_param : f . env in if ! Clflags . afl_instrument then Afl_instrument . instrument_function ( transl env body ) f . dbg else transl env body in let fun_codegen_options = if ! Clflags . optimize_for_s...
let rec transl_all_functions already_translated cont = match Cmmgen_state . next_function ( ) with | None -> cont , already_translated | Some f -> let sym = f . label in if String . Set . mem sym already_translated then transl_all_functions already_translated cont else begin transl_all_functions ( ...
let emit_constant_table symb elems = cdefine_symbol symb @ elems
let transl_clambda_constants ( constants : Clambda . preallocated_constant list ) cont = let c = ref cont in let emit_clambda_constant symbol global cst = let cst = emit_structured_constant ( symbol , global ) cst [ ] in c := ( Cdata cst ) :: ! c in List . iter ( fun { symbol ; expor...
let emit_cmm_data_items_for_constants cont = let c = ref cont in String . Map . iter ( fun symbol ( cst : Cmmgen_state . constant ) -> match cst with | Const_closure ( global , fundecls , clos_vars ) -> let cmm = emit_constant_closure ( symbol , global ) fundecls ( List . fold_right...
let transl_all_functions cont = let rec aux already_translated cont translated_functions = if Cmmgen_state . no_more_functions ( ) then cont , translated_functions else let translated_functions , already_translated = transl_all_functions already_translated translated_functions in aux already_translated con...
let compunit ( ulam , preallocated_blocks , constants ) = assert ( Cmmgen_state . no_more_functions ( ) ) ; let dbg = Debuginfo . none in Cmmgen_state . set_structured_constants constants ; let init_code = if ! Clflags . afl_instrument then Afl_instrument . instrument_initialiser ( t...
type is_global = Global | Local