text
stringlengths 12
786k
|
---|
let transl_class_rebind cl vf = try let obj_init = Ident . create_local " obj_init " and self = Ident . create_local " self " in let obj_init0 = lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = Lvar obj_init ; ap_args [ = Lvar self ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } in let _ , path_lam , obj_init ' = transl_class_rebind_0 self obj_init0 cl vf in let id = ( obj_init ' = lfunction [ self , Pgenval ] obj_init0 ) in if id then path_lam else let cla = Ident . create_local " class " and new_init = Ident . create_local " new_init " and env_init = Ident . create_local " env_init " and table = Ident . create_local " table " and envs = Ident . create_local " envs " in Llet ( Strict , Pgenval , new_init , lfunction [ obj_init , Pgenval ] obj_init ' , Llet ( Alias , Pgenval , cla , path_lam , Lprim ( Pmakeblock ( 0 , Immutable , None ) , [ mkappl ( Lvar new_init , [ lfield cla 0 ] ) ; lfunction [ table , Pgenval ] ( Llet ( Strict , Pgenval , env_init , mkappl ( lfield cla 1 , [ Lvar table ] ) , lfunction [ envs , Pgenval ] ( mkappl ( Lvar new_init , [ mkappl ( Lvar env_init , [ Lvar envs ] ) ] ) ) ) ) ; lfield cla 2 ; lfield cla 3 ] , Location . none ) ) ) with Exit -> lambda_unit
|
let rec module_path = function Lvar id -> let s = Ident . name id in s <> " " && s . [ 0 ] >= ' A ' && s . [ 0 ] <= ' Z ' | Lprim ( Pfield _ , [ p ] , _ ) -> module_path p | Lprim ( Pgetglobal _ , [ ] , _ ) -> true | _ -> false
|
let const_path local = function Lvar id -> not ( List . mem id local ) | Lconst _ -> true | Lfunction { kind = Curried ; body } -> let fv = free_variables body in List . for_all ( fun x -> not ( Ident . Set . mem x fv ) ) local | p -> module_path p
|
let rec builtin_meths self env env2 body = let const_path = const_path ( env :: self ) in let conv = function | p when const_path p -> " const " , [ p ] | Lprim ( Parrayrefu _ , [ Lvar s ; Lvar n ] , _ ) when List . mem s self -> " var " , [ Lvar n ] | Lprim ( Pfield n , [ Lvar e ] , _ ) when Ident . same e env -> " env " , [ Lvar env2 ; Lconst ( Const_pointer n ) ] | Lsend ( Self , met , Lvar s , [ ] , _ ) when List . mem s self -> " meth " , [ met ] | _ -> raise Not_found in match body with | Llet ( _str , _k , s ' , Lvar s , body ) when List . mem s self -> builtin_meths ( s ' :: self ) env env2 body | Lapply { ap_func = f ; ap_args = [ arg ] } when const_path f -> let s , args = conv arg in ( " app_ " ^ s , f :: args ) | Lapply { ap_func = f ; ap_args = [ arg ; p ] } when const_path f && const_path p -> let s , args = conv arg in ( " app_ " ^ s " ^ _const " , f :: args @ [ p ] ) | Lapply { ap_func = f ; ap_args = [ p ; arg ] } when const_path f && const_path p -> let s , args = conv arg in ( " app_const_ " ^ s , f :: p :: args ) | Lsend ( Self , Lvar n , Lvar s , [ arg ] , _ ) when List . mem s self -> let s , args = conv arg in ( " meth_app_ " ^ s , Lvar n :: args ) | Lsend ( Self , met , Lvar s , [ ] , _ ) when List . mem s self -> ( " get_meth " , [ met ] ) | Lsend ( Public , met , arg , [ ] , _ ) -> let s , args = conv arg in ( " send_ " ^ s , met :: args ) | Lsend ( Cached , met , arg , [ _ ; _ ] , _ ) -> let s , args = conv arg in ( " send_ " ^ s , met :: args ) | Lfunction { kind = Curried ; params = [ x , _ ] ; body } -> let rec enter self = function | Lprim ( Parraysetu _ , [ Lvar s ; Lvar n ; Lvar x ' ] , _ ) when Ident . same x x ' && List . mem s self -> ( " set_var " , [ Lvar n ] ) | Llet ( _str , _k , s ' , Lvar s , body ) when List . mem s self -> enter ( s ' :: self ) body | _ -> raise Not_found in enter self body | Lfunction _ -> raise Not_found | _ -> let s , args = conv body in ( " get_ " ^ s , args )
|
module M = struct open CamlinternalOO let builtin_meths self env env2 body = let builtin , args = builtin_meths self env env2 body in let tag = match builtin with " get_const " -> GetConst | " get_var " -> GetVar | " get_env " -> GetEnv | " get_meth " -> GetMeth | " set_var " -> SetVar | " app_const " -> AppConst | " app_var " -> AppVar | " app_env " -> AppEnv | " app_meth " -> AppMeth | " app_const_const " -> AppConstConst | " app_const_var " -> AppConstVar | " app_const_env " -> AppConstEnv | " app_const_meth " -> AppConstMeth | " app_var_const " -> AppVarConst | " app_env_const " -> AppEnvConst | " app_meth_const " -> AppMethConst | " meth_app_const " -> MethAppConst | " meth_app_var " -> MethAppVar | " meth_app_env " -> MethAppEnv | " meth_app_meth " -> MethAppMeth | " send_const " -> SendConst | " send_var " -> SendVar | " send_env " -> SendEnv | " send_meth " -> SendMeth | _ -> assert false in Lconst ( Const_pointer ( Obj . magic tag ) ) :: args end
|
let free_methods l = let fv = ref Ident . Set . empty in let rec free l = Lambda . iter_head_constructor free l ; match l with | Lsend ( Self , Lvar meth , _ , _ , _ ) -> fv := Ident . Set . add meth ! fv | Lsend _ -> ( ) | Lfunction { params } -> List . iter ( fun ( param , _ ) -> fv := Ident . Set . remove param ! fv ) params | Llet ( _str , _k , id , _arg , _body ) -> fv := Ident . Set . remove id ! fv | Lletrec ( decl , _body ) -> List . iter ( fun ( id , _exp ) -> fv := Ident . Set . remove id ! fv ) decl | Lstaticcatch ( _e1 , ( _ , vars ) , _e2 ) -> List . iter ( fun ( id , _ ) -> fv := Ident . Set . remove id ! fv ) vars | Ltrywith ( _e1 , exn , _e2 ) -> fv := Ident . Set . remove exn ! fv | Lfor ( v , _e1 , _e2 , _dir , _e3 ) -> fv := Ident . Set . remove v ! fv | Lassign _ | Lvar _ | Lconst _ | Lapply _ | Lprim _ | Lswitch _ | Lstringswitch _ | Lstaticraise _ | Lifthenelse _ | Lsequence _ | Lwhile _ | Levent _ | Lifused _ -> ( ) in free l ; ! fv
|
let transl_class ids cl_id pub_meths cl vflag = let rebind = transl_class_rebind cl vflag in if rebind <> lambda_unit then rebind else let tables = Ident . create_local ( Ident . name cl_id ^ " _tables " ) in let ( top_env , req ) = oo_add_class tables in let top = not req in let cl_env , llets = build_class_lets cl in let new_ids = if top then [ ] else Env . diff top_env cl_env in let env2 = Ident . create_local " env " in let meth_ids = get_class_meths cl in let subst env lam i0 new_ids ' = let fv = free_variables lam in let fv = List . fold_right Ident . Set . remove ! new_ids ' fv in method_ids := Ident . Set . diff ( Ident . Set . union ( free_methods lam ) ! method_ids ) meth_ids ; let new_ids = List . fold_right Ident . Set . add new_ids ! method_ids in let fv = Ident . Set . inter fv new_ids in new_ids ' := ! new_ids ' @ Ident . Set . elements fv ; let i = ref ( i0 - 1 ) in List . fold_left ( fun subst id -> incr i ; Ident . Map . add id ( lfield env ! i ) subst ) Ident . Map . empty ! new_ids ' in let new_ids_meths = ref [ ] in let no_env_update _ _ env = env in let msubst arr = function Lfunction { kind = Curried ; params = ( self , Pgenval ) :: args ; body } -> let env = Ident . create_local " env " in let body ' = if new_ids = [ ] then body else Lambda . subst no_env_update ( subst env body 0 new_ids_meths ) body in begin try if not arr || ! Clflags . debug then raise Not_found ; builtin_meths [ self ] env env2 ( lfunction args body ' ) with Not_found -> [ lfunction ( ( self , Pgenval ) :: args ) ( if not ( Ident . Set . mem env ( free_variables body ' ) ) then body ' else Llet ( Alias , Pgenval , env , Lprim ( Pfield_computed , [ Lvar self ; Lvar env2 ] , Location . none ) , body ' ) ) ] end | _ -> assert false in let new_ids_init = ref [ ] in let env1 = Ident . create_local " env " and env1 ' = Ident . create_local " env ' " in let copy_env self = if top then lambda_unit else Lifused ( env2 , Lprim ( Psetfield_computed ( Pointer , Assignment ) , [ Lvar self ; Lvar env2 ; Lvar env1 ' ] , Location . none ) ) and subst_env envs l lam = if top then lam else let lam = Lambda . subst no_env_update ( subst env1 lam 1 new_ids_init ) lam in Llet ( Alias , Pgenval , env1 , ( if l = [ ] then Lvar envs else lfield envs 0 ) , Llet ( Alias , Pgenval , env1 ' , ( if ! new_ids_init = [ ] then Lvar env1 else lfield env1 0 ) , lam ) ) in let cla = Ident . create_local " class " in let ( inh_init , obj_init ) = build_object_init_0 cla [ ] cl copy_env subst_env top ids in let inh_init ' = List . rev inh_init in let ( inh_init ' , cl_init ) = build_class_init cla true ( [ ] , [ ] ) inh_init ' obj_init msubst top cl in assert ( inh_init ' = [ ] ) ; let table = Ident . create_local " table " and class_init = Ident . create_local ( Ident . name cl_id ^ " _init " ) and env_init = Ident . create_local " env_init " and obj_init = Ident . create_local " obj_init " in let pub_meths = List . sort ( fun s s ' -> compare ( Btype . hash_variant s ) ( Btype . hash_variant s ' ) ) pub_meths in let tags = List . map Btype . hash_variant pub_meths in let rev_map = List . combine tags pub_meths in List . iter2 ( fun tag name -> let name ' = List . assoc tag rev_map in if name ' <> name then raise ( Error ( cl . cl_loc , Tags ( name , name ' ) ) ) ) tags pub_meths ; let ltable table lam = Llet ( Strict , Pgenval , table , mkappl ( oo_prim " create_table " , [ transl_meth_list pub_meths ] ) , lam ) and ldirect obj_init = Llet ( Strict , Pgenval , obj_init , cl_init , Lsequence ( mkappl ( oo_prim " init_class " , [ Lvar cla ] ) , mkappl ( Lvar obj_init , [ lambda_unit ] ) ) ) in if top && ids = [ ] then llets ( ltable cla ( ldirect obj_init ) ) else let concrete = ( vflag = Concrete ) and lclass lam = let cl_init = llets ( Lfunction { kind = Curried ; attr = default_function_attribute ; loc = Location . none ; return = Pgenval ; params = [ cla , Pgenval ] ; body = cl_init } ) in Llet ( Strict , Pgenval , class_init , cl_init , lam ( free_variables cl_init ) ) and lbody fv = if List . for_all ( fun id -> not ( Ident . Set . mem id fv ) ) ids then mkappl ( oo_prim " make_class " , [ transl_meth_list pub_meths ; Lvar class_init ] ) else ltable table ( Llet ( Strict , Pgenval , env_init , mkappl ( Lvar class_init , [ Lvar table ] ) , Lsequence ( mkappl ( oo_prim " init_class " , [ Lvar table ] ) , Lprim ( Pmakeblock ( 0 , Immutable , None ) , [ mkappl ( Lvar env_init , [ lambda_unit ] ) ; Lvar class_init ; Lvar env_init ; lambda_unit ] , Location . none ) ) ) ) and lbody_virt lenvs = Lprim ( Pmakeblock ( 0 , Immutable , None ) , [ lambda_unit ; Lfunction { kind = Curried ; attr = default_function_attribute ; loc = Location . none ; return = Pgenval ; params = [ cla , Pgenval ] ; body = cl_init } ; lambda_unit ; lenvs ] , Location . none ) in if top && concrete then lclass lbody else if top then llets ( lbody_virt lambda_unit ) else let envs = Ident . create_local " envs " and cached = Ident . create_local " cached " in let lenvs = if ! new_ids_meths = [ ] && ! new_ids_init = [ ] && inh_init = [ ] then lambda_unit else Lvar envs in let lenv = let menv = if ! new_ids_meths = [ ] then lambda_unit else Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun id -> Lvar id ) ! new_ids_meths , Location . none ) in if ! new_ids_init = [ ] then menv else Lprim ( Pmakeblock ( 0 , Immutable , None ) , menv :: List . map ( fun id -> Lvar id ) ! new_ids_init , Location . none ) and linh_envs = List . map ( fun ( _ , path_lam , _ ) -> Lprim ( Pfield 3 , [ path_lam ] , Location . none ) ) ( List . rev inh_init ) in let make_envs lam = Llet ( StrictOpt , Pgenval , envs , ( if linh_envs = [ ] then lenv else Lprim ( Pmakeblock ( 0 , Immutable , None ) , lenv :: linh_envs , Location . none ) ) , lam ) and def_ids cla lam = Llet ( StrictOpt , Pgenval , env2 , mkappl ( oo_prim " new_variable " , [ Lvar cla ; transl_label " " ] ) , lam ) in let inh_paths = List . filter ( fun ( path , _ , _ ) -> List . mem ( Path . head path ) new_ids ) inh_init in let inh_keys = List . map ( fun ( _ , path_lam , _ ) -> Lprim ( Pfield 1 , [ path_lam ] , Location . none ) ) inh_paths in let lclass lam = Llet ( Strict , Pgenval , class_init , Lfunction { kind = Curried ; params = [ cla , Pgenval ] ; return = Pgenval ; attr = default_function_attribute ; loc = Location . none ; body = def_ids cla cl_init } , lam ) and lcache lam = if inh_keys = [ ] then Llet ( Alias , Pgenval , cached , Lvar tables , lam ) else Llet ( Strict , Pgenval , cached , mkappl ( oo_prim " lookup_tables " , [ Lvar tables ; Lprim ( Pmakeblock ( 0 , Immutable , None ) , inh_keys , Location . none ) ] ) , lam ) and lset cached i lam = Lprim ( Psetfield ( i , Pointer , Assignment ) , [ Lvar cached ; lam ] , Location . none ) in let ldirect ( ) = ltable cla ( Llet ( Strict , Pgenval , env_init , def_ids cla cl_init , Lsequence ( mkappl ( oo_prim " init_class " , [ Lvar cla ] ) , lset cached 0 ( Lvar env_init ) ) ) ) and lclass_virt ( ) = lset cached 0 ( Lfunction { kind = Curried ; attr = default_function_attribute ; loc = Location . none ; return = Pgenval ; params = [ cla , Pgenval ] ; body = def_ids cla cl_init ; } ) in let lupdate_cache = if ids = [ ] then ldirect ( ) else if not concrete then lclass_virt ( ) else lclass ( mkappl ( oo_prim " make_class_store " , [ transl_meth_list pub_meths ; Lvar class_init ; Lvar cached ] ) ) in let lcheck_cache = if ! Clflags . native_code && ! Clflags . afl_instrument then lupdate_cache else Lifthenelse ( lfield cached 0 , lambda_unit , lupdate_cache ) in llets ( lcache ( Lsequence ( lcheck_cache , make_envs ( if ids = [ ] then mkappl ( lfield cached 0 , [ lenvs ] ) else Lprim ( Pmakeblock ( 0 , Immutable , None ) , ( if concrete then [ mkappl ( lfield cached 0 , [ lenvs ] ) ; lfield cached 1 ; lfield cached 0 ; lenvs ] else [ lambda_unit ; lfield cached 0 ; lambda_unit ; lenvs ] ) , Location . none ) ) ) ) ) let _arity = List . length ci . ci_params in let pub_meths = m in let cl = ci . ci_expr in let vflag = vf in ) *
|
let transl_class ids id pub_meths cl vf = oo_wrap cl . cl_env false ( transl_class ids id pub_meths cl ) vf
|
let ( ) = transl_object := ( fun id meths cl -> transl_class [ ] id meths cl Concrete )
|
let report_error ppf = function | Tags ( lab1 , lab2 ) -> fprintf ppf " Method labels ` % s ' and ` % s ' are incompatible . @ % s " lab1 lab2 " Change one of them . "
|
let ( ) = Location . register_error_of_exn ( function | Error ( loc , err ) -> Some ( Location . error_of_printer ~ loc report_error err ) | _ -> None )
|
let int n = Lconst ( Const_base ( Const_int n ) )
|
type binding = { let_kind : let_kind ; value_kind : value_kind ; var : Ident . t ; init : lambda }
|
let binding let_kind value_kind var init = { let_kind ; value_kind ; var ; init }
|
let gen_binding { let_kind ; value_kind ; var ; init } body = Llet ( let_kind , value_kind , var , init , body )
|
let gen_bindings bindings body = List . fold_right gen_binding bindings body
|
let valuekind_of_arraykind = function | Pgenarray -> Pgenval | Paddrarray | Pintarray -> Pintval | Pfloatarray -> Pfloatval
|
let transl_arr_clause ~ transl_exp ~ scopes ~ loc clause body = let len_var = Ident . create_local " len_var " in let bindings , for_ = match clause with | In ( pat , e2 ) -> let in_var = Ident . create_local " in_var " in let in_kind = Typeopt . array_kind e2 in let in_binding = binding Strict Pgenval in_var ( transl_exp ~ scopes e2 ) in let len_binding = let init = Lprim ( ( Parraylength ( in_kind ) ) , [ Lvar ( in_var ) ] , loc ) in binding Alias Pintval len_var init in let index = Ident . create_local " index " in let for_ = Lfor ( index , ( int 0 ) , Lprim ( Psubint , [ Lvar ( len_var ) ; int 1 ] , loc ) , Upto , Matching . for_let ~ scopes pat . pat_loc ( Lprim ( Parrayrefu ( in_kind ) , [ Lvar ( in_var ) ; Lvar ( index ) ] , loc ) ) pat ( valuekind_of_arraykind in_kind ) body ) in [ in_binding ; len_binding ] , for_ | From_to ( id , _ , e2 , e3 , dir ) -> let from_var = Ident . create_local " from " in let from_binding = binding Strict Pintval from_var ( transl_exp ~ scopes e2 ) in let to_var = Ident . create_local " to " in let to_binding = binding Strict Pintval to_var ( transl_exp ~ scopes e3 ) in let low , high = match dir with | Upto -> Lvar from_var , Lvar to_var | Downto -> Lvar to_var , Lvar from_var in let len_binding = let init = Lprim ( Psubint , [ Lprim ( Paddint , [ high ; int 1 ] , loc ) ; low ] , loc ) in binding Alias Pintval len_var init in let for_ = Lfor ( id , Lvar from_var , Lvar to_var , dir , body ) in [ from_binding ; to_binding ; len_binding ] , for_ in bindings , len_var , for_
|
let iterate_arr_block ~ transl_exp ~ loc ~ scopes { clauses ; guard } length_var body = let body = match guard with | None -> body | Some guard -> Lifthenelse ( transl_exp ~ scopes guard , body , lambda_unit , Pintval ) in let body , length_opt , bindings = List . fold_left ( fun ( body , length , bindings ) clause -> let new_bindings , new_length_var , body = transl_arr_clause ~ transl_exp ~ scopes ~ loc clause body in let rev_bindings = new_bindings @ bindings in let length = match length with | None -> Lvar new_length_var | Some length -> Lprim ( Pmulint , [ Lvar new_length_var ; length ] , loc ) in body , Some length , rev_bindings ) ( body , None , [ ] ) clauses in let length = Option . value length_opt ~ default ( : int 0 ) in let length_binding = binding Alias Pintval length_var length in let bindings = List . append bindings [ length_binding ] in bindings , body
|
let make_array_prim ~ loc size init = let prim = Primitive . simple ~ name " : caml_make_vect " ~ arity : 2 ~ alloc : true in Lprim ( Pccall prim , [ size ; init ] , loc )
|
let make_floatarray_prim ~ loc size = let prim = Primitive . simple ~ name " : caml_make_float_vect " ~ arity : 1 ~ alloc : true in Lprim ( Pccall prim , [ size ] , loc )
|
let blit_array_prim ~ loc src src_pos dst dst_pos len = let prim_blit_arr = Primitive . simple ~ name " : caml_array_blit " ~ arity : 5 ~ alloc : true in Lprim ( Pccall prim_blit_arr , [ src ; src_pos ; dst ; dst_pos ; len ] , loc )
|
let make_array ~ loc ~ kind ~ size ~ array = match kind with | Pgenarray -> let init = Lprim ( Pmakearray ( Pgenarray , Immutable , alloc_heap ) , [ ] , loc ) in binding Variable Pgenval array init | Pintarray | Paddrarray -> let init = make_array_prim ~ loc size ( int 0 ) in binding Strict Pgenval array init | Pfloatarray -> let init = make_floatarray_prim ~ loc size in binding Strict Pgenval array init
|
let init_array_elem ~ loc ~ kind ~ size ~ array ~ index ~ value = let set_elem = Lprim ( Parraysetu kind , [ Lvar array ; Lvar index ; Lvar value ] , loc ) in match kind with | Pgenarray -> let is_first_iteration = Lprim ( Pintcomp Ceq , [ Lvar index ; int 0 ] , loc ) in let make_array = Lassign ( array , make_array_prim ~ loc size ( Lvar value ) ) in Lifthenelse ( is_first_iteration , make_array , set_elem , Pintval ) | Pintarray | Paddrarray | Pfloatarray -> set_elem
|
let init_array_elems ~ loc ~ kind ~ size ~ array ~ index ~ src ~ len = let blit = blit_array_prim ~ loc ( Lvar src ) ( int 0 ) ( Lvar array ) ( Lvar index ) ( Lvar len ) in match kind with | Pgenarray -> let is_first_iteration = Lprim ( Pintcomp Ceq , [ Lvar index ; int 0 ] , loc ) in let is_not_empty = Lprim ( Pintcomp ( Cne ) , [ Lvar len ; int 0 ] , loc ) in let first_elem = Lprim ( Parrayrefu kind , [ Lvar src ; int 0 ] , loc ) in let make_array = Lassign ( array , make_array_prim ~ loc size first_elem ) in Lsequence ( Lifthenelse ( is_first_iteration , Lifthenelse ( is_not_empty , make_array , lambda_unit , Pintval ) , lambda_unit , Pintval ) , blit ) | Pintarray | Paddrarray | Pfloatarray -> blit
|
let make_counter counter = binding Variable Pintval counter ( int 0 )
|
let increment_counter ~ loc counter step = Lassign ( counter , Lprim ( Paddint , [ Lvar counter ; step ] , loc ) )
|
type block_lambda = | Without_size of { body : lambda } | With_size of { body : lambda ; raise_count : int }
|
let transl_arr_block ~ transl_exp ~ loc ~ scopes global_counter body array_kind value_kind block = let length_var = Ident . create_local " len " in let size = match body with | Without_size _ -> Lvar length_var | With_size _ -> Lprim ( Pmulint , [ Lvar length_var ; int 2 ] , loc ) in let result_array_var = Ident . create_local " arr " in let result_array_binding = make_array ~ loc ~ kind : array_kind ~ size ~ array : result_array_var in let counter_var = Ident . create_local " counter " in let counter_binding = make_counter counter_var in let elem_var = Ident . create_local " elem " in let init_elem = init_array_elem ~ loc ~ kind : array_kind ~ size ~ array : result_array_var ~ index : counter_var ~ value : elem_var in let set_result = match body with | Without_size { body } -> Llet ( Strict , value_kind , elem_var , body , Lsequence ( init_elem , increment_counter ~ loc counter_var ( int 1 ) ) ) | With_size { body ; raise_count } -> let elem_len_var = Ident . create_local " len " in let set_len = Lprim ( Parraysetu Paddrarray , [ Lvar result_array_var ; Lprim ( Paddint , [ Lvar counter_var ; int 1 ] , loc ) ; Lvar elem_len_var ] , loc ) in Lstaticcatch ( body , ( raise_count , [ ( elem_var , Pgenval ) ; ( elem_len_var , Pintval ) ] ) , Lsequence ( init_elem , Lsequence ( set_len , increment_counter ~ loc counter_var ( int 2 ) ) ) , Pintval ) in let bindings , loops = iterate_arr_block ~ transl_exp ~ loc ~ scopes block length_var set_result in let bindings = bindings @ [ result_array_binding ; counter_binding ] in let body = match global_counter with | None -> loops | Some global_counter_var -> let len = match body with | Without_size _ -> Lvar counter_var | With_size _ -> Lprim ( Pdivint Unsafe , [ Lvar counter_var ; int 2 ] , loc ) in Lsequence ( loops , increment_counter ~ loc global_counter_var len ) in match block . guard with | None -> let body = gen_bindings bindings ( Lsequence ( body , Lvar result_array_var ) ) in Without_size { body } | Some _ -> let raise_count = next_raise_count ( ) in let return = Lstaticraise ( raise_count , [ Lvar result_array_var ; Lvar counter_var ] ) in let body = gen_bindings bindings ( Lsequence ( body , return ) ) in With_size { body ; raise_count }
|
let sub_array ~ loc src src_pos len = let prim = Primitive . simple ~ name " : caml_array_sub " ~ arity : 3 ~ alloc : true in Lprim ( Pccall prim , [ src ; src_pos ; len ] , loc )
|
let transl_single_arr_block ~ transl_exp ~ loc ~ scopes block body array_kind value_kind = let body = transl_arr_block ~ transl_exp ~ loc ~ scopes None ( Without_size { body } ) array_kind value_kind block in match body with | Without_size { body } -> body | With_size { body ; raise_count } -> let array_var = Ident . create_local " array " in let len_var = Ident . create_local " len " in Lstaticcatch ( body , ( raise_count , [ ( array_var , Pgenval ) ; ( len_var , Pintval ) ] ) , sub_array ~ loc ( Lvar array_var ) ( int 0 ) ( Lvar len_var ) , Pintval )
|
type intermediate_array_shape = | Array_of_elements | Array_of_arrays of intermediate_array_shape | Array_of_filtered_arrays of intermediate_array_shape
|
let concat_arrays ~ loc arr kind shape global_count_var = let res_var = Ident . create_local " res " in let res_binding = make_array ~ loc ~ kind ~ size ( : Lvar global_count_var ) ~ array : res_var in let counter_var = Ident . create_local " counter " in let counter_binding = make_counter counter_var in let rec loop shape arr_var len_var = let kind = match shape with | Array_of_elements -> kind | Array_of_arrays _ | Array_of_filtered_arrays _ -> Paddrarray in let len_var , bindings = match len_var with | Some var -> var , [ ] | None -> let var = Ident . create_local " len " in let init = Lprim ( ( Parraylength kind ) , [ Lvar ( arr_var ) ] , loc ) in let binding = binding Alias Pintval var init in var , [ binding ] in match shape with | Array_of_elements -> gen_bindings bindings ( Lsequence ( init_array_elems ~ loc ~ kind ~ size ( : Lvar global_count_var ) ~ array : res_var ~ index : counter_var ~ src : arr_var ~ len : len_var , increment_counter ~ loc counter_var ( Lvar len_var ) ) ) | Array_of_arrays shape -> let index_var = Ident . create_local " index " in let sub_arr_var = Ident . create_local " arr " in let last_index = Lprim ( Psubint , [ Lvar len_var ; int 1 ] , loc ) in let sub_arr = Lprim ( Parrayrefu kind , [ Lvar arr_var ; Lvar index_var ] , loc ) in gen_bindings bindings ( Lfor ( index_var , int 0 , last_index , Upto , Llet ( Strict , Pgenval , sub_arr_var , sub_arr , loop shape sub_arr_var None ) ) ) | Array_of_filtered_arrays shape -> let index_var = Ident . create_local " index " in let index_binding = make_counter index_var in let sub_arr_var = Ident . create_local " arr " in let sub_arr = Lprim ( Parrayrefu kind , [ Lvar arr_var ; Lvar index_var ] , loc ) in let sub_arr_len_var = Ident . create_local " len " in let sub_arr_len = Lprim ( Parrayrefu kind , [ Lvar arr_var ; Lprim ( Paddint , [ Lvar index_var ; int 1 ] , loc ) ] , loc ) in gen_bindings bindings ( gen_binding index_binding ( Lwhile ( Lprim ( Pintcomp Clt , [ Lvar index_var ; Lvar len_var ] , loc ) , Lsequence ( Llet ( Strict , Pgenval , sub_arr_var , sub_arr , Llet ( Strict , Pintval , sub_arr_len_var , sub_arr_len , loop shape sub_arr_var ( Some sub_arr_len_var ) ) ) , increment_counter ~ loc index_var ( int 2 ) ) ) ) ) in match arr with | Without_size { body } -> let array_var = Ident . create_local " array " in Llet ( Strict , Pgenval , array_var , body , gen_binding res_binding ( gen_binding counter_binding ( Lsequence ( loop shape array_var None , Lvar res_var ) ) ) ) | With_size { body ; raise_count } -> let array_var = Ident . create_local " array " in let len_var = Ident . create_local " len " in Lstaticcatch ( body , ( raise_count , [ ( array_var , Pgenval ) ; ( len_var , Pintval ) ] ) , gen_binding res_binding ( gen_binding counter_binding ( ( Lsequence ( loop shape array_var ( Some len_var ) , Lvar res_var ) ) ) ) , Pgenval )
|
let transl_arr_comprehension ~ transl_exp ~ loc ~ scopes ~ array_kind exp blocks = let body = transl_exp ~ scopes exp in let value_kind = Typeopt . value_kind exp . exp_env exp . exp_type in match blocks with | [ ] -> assert false | [ block ] -> transl_single_arr_block ~ transl_exp ~ loc ~ scopes block body array_kind value_kind | inner_block :: rest -> let counter_var = Ident . create_local " counter " in let counter_binding = make_counter counter_var in let body = transl_arr_block ~ transl_exp ~ loc ~ scopes ( Some counter_var ) ( Without_size { body } ) array_kind value_kind inner_block in let shape , body = List . fold_left ( fun ( shape , body ) block -> let shape = match body with | Without_size _ -> Array_of_arrays shape | With_size _ -> Array_of_filtered_arrays shape in let body = transl_arr_block ~ transl_exp ~ loc ~ scopes None body Paddrarray Pgenval block in shape , body ) ( Array_of_elements , body ) rest in gen_binding counter_binding ( concat_arrays ~ loc body array_kind shape counter_var )
|
let from_to_comp_prim ~ dir = let function_name = match dir with | Upto -> " map_from_to_cons " | Downto -> " map_from_downto_cons " in Lambda . transl_prim " CamlinternalComprehension " function_name
|
let in_comp_prim ( ) = Lambda . transl_prim " CamlinternalComprehension " " map_cons "
|
let comp_rev ( ) = Lambda . transl_prim " CamlinternalComprehension " " rev "
|
let transl_list_comp type_comp body acc_var mats ~ transl_exp ~ scopes ~ loc = let new_acc = Ident . create_local " acc " in let param , pval , args , func , body , mats = match type_comp with | From_to ( param , _ , e2 , e3 , dir ) -> let pval = Pintval in let from_var = Ident . create_local " from " in let to_var = Ident . create_local " to_ " in let args = [ Lvar ( from_var ) ; Lvar ( to_var ) ; Lvar ( new_acc ) ] in let func = from_to_comp_prim ~ dir in let mats = ( from_var , transl_exp ~ scopes e2 ) ( :: to_var , transl_exp ~ scopes e3 ) :: mats in param , pval , args , func , body , mats | In ( pat , in_ ) -> let pat_id = Ident . create_local " pat " in let pval = Typeopt . value_kind pat . pat_env pat . pat_type in let in_var = Ident . create_local " in_var " in let args = [ Lvar ( in_var ) ; Lvar ( new_acc ) ] in let func = in_comp_prim ( ) in let body = Matching . for_let ~ scopes pat . pat_loc ( Lvar ( pat_id ) ) pat pval body in let mats = ( in_var , transl_exp ~ scopes in_ ) :: mats in pat_id , pval , args , func , body , mats in let fn = Lfunction { kind = Curried { nlocal = 0 } ; params = [ param , pval ; acc_var , Pgenval ] ; return = Pgenval ; attr = default_function_attribute ; loc = loc ; body = body ; mode = alloc_heap ; region = true } in Lapply { ap_loc = loc ; ap_region_close = Rc_normal ; ap_mode = alloc_heap ; ap_func = func ; ap_args = fn :: args ; ap_tailcall = Default_tailcall ; ap_inlined = Default_inlined ; ap_specialised = Default_specialise ; ap_probe = None ; } , new_acc , mats
|
let transl_list_comprehension ~ transl_exp ~ loc ~ scopes body blocks = let acc_var = Ident . create_local " acc " in let value_kind = Typeopt . value_kind body . exp_env body . exp_type in let bdy = Lprim ( Pmakeblock ( 0 , Immutable , None , alloc_heap ) , [ ( transl_exp ~ scopes body ) ; Lvar ( acc_var ) ] , loc ) in let res_list , res_var = List . fold_left ( fun ( body , acc_var ) block -> let body = match block . guard with | None -> body | Some guard -> Lifthenelse ( ( transl_exp ~ scopes guard ) , body , Lvar ( acc_var ) , value_kind ) in let body , acc_var , materialize = List . fold_left ( fun ( body , acc_var , mats ) el -> transl_list_comp ~ transl_exp ~ scopes ~ loc el body acc_var mats ) ( body , acc_var , [ ] ) block . clauses in let body = List . fold_right ( fun ( id , arr ) body -> Llet ( Strict , Pgenval , id , arr , body ) ) materialize body in body , acc_var ) ( bdy , acc_var ) blocks in Llet ( Alias , Pintval , res_var , int 0 , Lapply { ap_loc = loc ; ap_func = comp_rev ( ) ; ap_args [ = res_list ] ; ap_region_close = Rc_normal ; ap_mode = alloc_heap ; ap_tailcall = Default_tailcall ; ap_inlined = Default_inlined ; ap_specialised = Default_specialise ; ap_probe = None ; } )
|
type error = Free_super_var | Unreachable_reached
|
let transl_module = ref ( ( fun _cc _rootpath _modl -> assert false ) : module_coercion -> Path . t option -> module_expr -> lambda )
|
let transl_object = ref ( fun _id _s _cl -> assert false : Ident . t -> string list -> class_expr -> lambda )
|
let prim_fresh_oo_id = Pccall ( Primitive . simple ~ name " : caml_fresh_oo_id " ~ arity : 1 ~ alloc : false )
|
let transl_extension_constructor env path ext = let path = Printtyp . wrap_printing_env env ~ error : true ( fun ( ) -> Option . map ( Printtyp . rewrite_double_underscore_paths env ) path ) in let name = match path , ! Clflags . for_package with None , _ -> Ident . name ext . ext_id | Some p , None -> Path . name p | Some p , Some pack -> Printf . sprintf " % s . % s " pack ( Path . name p ) in let loc = ext . ext_loc in match ext . ext_kind with Text_decl _ -> Lprim ( Pmakeblock ( Obj . object_tag , Immutable , None ) , [ Lconst ( Const_base ( Const_string ( name , None ) ) ) ; Lprim ( prim_fresh_oo_id , [ Lconst ( Const_base ( Const_int 0 ) ) ] , loc ) ] , loc ) | Text_rebind ( path , _lid ) -> transl_extension_path loc env path
|
let extract_constant = function Lconst sc -> sc | _ -> raise Not_constant
|
let extract_float = function Const_base ( Const_float f ) -> f | _ -> fatal_error " Translcore . extract_float "
|
type binding = | Bind_value of value_binding list | Bind_module of Ident . t * string option loc * module_presence * module_expr
|
let rec push_defaults loc bindings cases partial = match cases with [ { c_lhs = pat ; c_guard = None ; c_rhs { = exp_desc = Texp_function { arg_label ; param ; cases ; partial ; } } as exp } ] -> let cases = push_defaults exp . exp_loc bindings cases partial in [ { c_lhs = pat ; c_guard = None ; c_rhs { = exp with exp_desc = Texp_function { arg_label ; param ; cases ; partial ; } } } ] | [ { c_lhs = pat ; c_guard = None ; c_rhs { = exp_attributes [ { = Parsetree . attr_name = { txt " =# default " } ; _ } ] ; exp_desc = Texp_let ( Nonrecursive , binds , ( { exp_desc = Texp_function _ } as e2 ) ) } } ] -> push_defaults loc ( Bind_value binds :: bindings ) [ { c_lhs = pat ; c_guard = None ; c_rhs = e2 } ] partial | [ { c_lhs = pat ; c_guard = None ; c_rhs { = exp_attributes [ { = Parsetree . attr_name = { txt " =# modulepat " } ; _ } ] ; exp_desc = Texp_letmodule ( Some id , name , pres , mexpr , ( { exp_desc = Texp_function _ } as e2 ) ) } } ] -> push_defaults loc ( Bind_module ( id , name , pres , mexpr ) :: bindings ) [ { c_lhs = pat ; c_guard = None ; c_rhs = e2 } ] partial | [ case ] -> let exp = List . fold_left ( fun exp binds -> { exp with exp_desc = match binds with | Bind_value binds -> Texp_let ( Nonrecursive , binds , exp ) | Bind_module ( id , name , pres , mexpr ) -> Texp_letmodule ( Some id , name , pres , mexpr , exp ) } ) case . c_rhs bindings in [ { case with c_rhs = exp } ] | { c_lhs = pat ; c_rhs = exp ; c_guard = _ } :: _ when bindings <> [ ] -> let param = Typecore . name_cases " param " cases in let desc = { val_type = pat . pat_type ; val_kind = Val_reg ; val_attributes = [ ] ; Types . val_loc = Location . none ; } in let env = Env . add_value param desc exp . exp_env in let name = Ident . name param in let exp = { exp with exp_loc = loc ; exp_env = env ; exp_desc = Texp_match ( { exp with exp_type = pat . pat_type ; exp_env = env ; exp_desc = Texp_ident ( Path . Pident param , mknoloc ( Longident . Lident name ) , desc ) } , cases , partial ) } in push_defaults loc bindings [ { c_lhs { = pat with pat_desc = Tpat_var ( param , mknoloc name ) } ; c_guard = None ; c_rhs = exp } ] Total | _ -> cases
|
let event_function exp lam = if ! Clflags . debug && not ! Clflags . native_code then let repr = Some ( ref 0 ) in let ( info , body ) = lam repr in ( info , Levent ( body , { lev_loc = exp . exp_loc ; lev_kind = Lev_function ; lev_repr = repr ; lev_env = exp . exp_env } ) ) else lam None
|
let assert_failed exp = let slot = transl_extension_path Location . none Env . initial_safe_string Predef . path_assert_failure in let ( fname , line , char ) = Location . get_pos_info exp . exp_loc . Location . loc_start in Lprim ( Praise Raise_regular , [ event_after exp ( Lprim ( Pmakeblock ( 0 , Immutable , None ) , [ slot ; Lconst ( Const_block ( 0 , [ Const_base ( Const_string ( fname , None ) ) ; Const_base ( Const_int line ) ; Const_base ( Const_int char ) ] ) ) ] , exp . exp_loc ) ) ] , exp . exp_loc ) ; ;
|
let rec cut n l = if n = 0 then ( [ ] , l ) else match l with [ ] -> failwith " Translcore . cut " | a :: l -> let ( l1 , l2 ) = cut ( n - 1 ) l in ( a :: l1 , l2 )
|
let rec iter_exn_names f pat = match pat . pat_desc with | Tpat_var ( id , _ ) -> f id | Tpat_alias ( p , id , _ ) -> f id ; iter_exn_names f p | _ -> ( )
|
let transl_ident loc env ty path desc = match desc . val_kind with | Val_prim p -> Translprim . transl_primitive loc p env ty ( Some path ) | Val_anc _ -> raise ( Error ( loc , Free_super_var ) ) | Val_reg | Val_self _ -> transl_value_path loc env path | _ -> fatal_error " Translcore . transl_exp : bad Texp_ident "
|
let rec transl_exp e = List . iter ( Translattribute . check_attribute e ) e . exp_attributes ; let eval_once = match e . exp_desc with Texp_function _ | Texp_for _ | Texp_while _ -> false | _ -> true in if eval_once then transl_exp0 e else Translobj . oo_wrap e . exp_env true transl_exp0 e match e . exp_desc with | Texp_ident ( path , _ , desc ) -> transl_ident e . exp_loc e . exp_env e . exp_type path desc | Texp_constant cst -> Lconst ( Const_base cst ) | Texp_let ( rec_flag , pat_expr_list , body ) -> transl_let rec_flag pat_expr_list ( event_before body ( transl_exp body ) ) | Texp_function { arg_label = _ ; param ; cases ; partial ; } -> let ( ( kind , params , return ) , body ) = event_function e ( function repr -> let pl = push_defaults e . exp_loc [ ] cases partial in let return_kind = function_return_value_kind e . exp_env e . exp_type in transl_function e . exp_loc return_kind ! Clflags . native_code repr partial param pl ) in let attr = default_function_attribute in let loc = e . exp_loc in let lam = Lfunction { kind ; params ; return ; body ; attr ; loc } in Translattribute . add_function_attributes lam loc e . exp_attributes | Texp_apply ( { exp_desc = Texp_ident ( path , _ , { val_kind = Val_prim p } ) ; exp_type = prim_type } as funct , oargs ) when List . length oargs >= p . prim_arity && List . for_all ( fun ( _ , arg ) -> arg <> None ) oargs -> let argl , extra_args = cut p . prim_arity oargs in let arg_exps = List . map ( function _ , Some x -> x | _ -> assert false ) argl in let args = transl_list arg_exps in let prim_exp = if extra_args = [ ] then Some e else None in let lam = Translprim . transl_primitive_application e . exp_loc p e . exp_env prim_type path prim_exp args arg_exps in if extra_args = [ ] then lam else begin let should_be_tailcall , funct = Translattribute . get_tailcall_attribute funct in let inlined , funct = Translattribute . get_and_remove_inlined_attribute funct in let specialised , funct = Translattribute . get_and_remove_specialised_attribute funct in let e = { e with exp_desc = Texp_apply ( funct , oargs ) } in event_after e ( transl_apply ~ should_be_tailcall ~ inlined ~ specialised lam extra_args e . exp_loc ) end | Texp_apply ( funct , oargs ) -> let should_be_tailcall , funct = Translattribute . get_tailcall_attribute funct in let inlined , funct = Translattribute . get_and_remove_inlined_attribute funct in let specialised , funct = Translattribute . get_and_remove_specialised_attribute funct in let e = { e with exp_desc = Texp_apply ( funct , oargs ) } in event_after e ( transl_apply ~ should_be_tailcall ~ inlined ~ specialised ( transl_exp funct ) oargs e . exp_loc ) | Texp_match ( arg , pat_expr_list , partial ) -> transl_match e arg pat_expr_list partial | Texp_try ( body , pat_expr_list ) -> let id = Typecore . name_cases " exn " pat_expr_list in Ltrywith ( transl_exp body , id , Matching . for_trywith ( Lvar id ) ( transl_cases_try pat_expr_list ) ) | Texp_tuple el -> let ll , shape = transl_list_with_shape el in begin try Lconst ( Const_block ( 0 , List . map extract_constant ll ) ) with Not_constant -> Lprim ( Pmakeblock ( 0 , Immutable , Some shape ) , ll , e . exp_loc ) end | Texp_construct ( _ , cstr , args ) -> let ll , shape = transl_list_with_shape args in if cstr . cstr_inlined <> None then begin match ll with | [ x ] -> x | _ -> assert false end else begin match cstr . cstr_tag with Cstr_constant n -> Lconst ( Const_pointer n ) | Cstr_unboxed -> ( match ll with [ v ] -> v | _ -> assert false ) | Cstr_block n -> begin try Lconst ( Const_block ( n , List . map extract_constant ll ) ) with Not_constant -> Lprim ( Pmakeblock ( n , Immutable , Some shape ) , ll , e . exp_loc ) end | Cstr_extension ( path , is_const ) -> let lam = transl_extension_path e . exp_loc e . exp_env path in if is_const then lam else Lprim ( Pmakeblock ( 0 , Immutable , Some ( Pgenval :: shape ) ) , lam :: ll , e . exp_loc ) end | Texp_extension_constructor ( _ , path ) -> transl_extension_path e . exp_loc e . exp_env path | Texp_variant ( l , arg ) -> let tag = Btype . hash_variant l in begin match arg with None -> Lconst ( Const_pointer tag ) | Some arg -> let lam = transl_exp arg in try Lconst ( Const_block ( 0 , [ Const_base ( Const_int tag ) ; extract_constant lam ] ) ) with Not_constant -> Lprim ( Pmakeblock ( 0 , Immutable , None ) , [ Lconst ( Const_base ( Const_int tag ) ) ; lam ] , e . exp_loc ) end | Texp_record { fields ; representation ; extended_expression } -> transl_record e . exp_loc e . exp_env fields representation extended_expression | Texp_field ( arg , _ , lbl ) -> let targ = transl_exp arg in begin match lbl . lbl_repres with Record_regular | Record_inlined _ -> Lprim ( Pfield lbl . lbl_pos , [ targ ] , e . exp_loc ) | Record_unboxed _ -> targ | Record_float -> Lprim ( Pfloatfield lbl . lbl_pos , [ targ ] , e . exp_loc ) | Record_extension _ -> Lprim ( Pfield ( lbl . lbl_pos + 1 ) , [ targ ] , e . exp_loc ) end | Texp_setfield ( arg , _ , lbl , newval ) -> let access = match lbl . lbl_repres with Record_regular | Record_inlined _ -> Psetfield ( lbl . lbl_pos , maybe_pointer newval , Assignment ) | Record_unboxed _ -> assert false | Record_float -> Psetfloatfield ( lbl . lbl_pos , Assignment ) | Record_extension _ -> Psetfield ( lbl . lbl_pos + 1 , maybe_pointer newval , Assignment ) in Lprim ( access , [ transl_exp arg ; transl_exp newval ] , e . exp_loc ) | Texp_array expr_list -> let kind = array_kind e in let ll = transl_list expr_list in begin try if List . length ll <= use_dup_for_constant_arrays_bigger_than then begin raise Not_constant end ; begin match List . map extract_constant ll with | exception Not_constant when kind = Pfloatarray -> let imm_array = Lprim ( Pmakearray ( kind , Immutable ) , ll , e . exp_loc ) in Lprim ( Pduparray ( kind , Mutable ) , [ imm_array ] , e . exp_loc ) | cl -> let imm_array = match kind with | Paddrarray | Pintarray -> Lconst ( Const_block ( 0 , cl ) ) | Pfloatarray -> Lconst ( Const_float_array ( List . map extract_float cl ) ) | Pgenarray -> raise Not_constant in Lprim ( Pduparray ( kind , Mutable ) , [ imm_array ] , e . exp_loc ) end with Not_constant -> Lprim ( Pmakearray ( kind , Mutable ) , ll , e . exp_loc ) end | Texp_ifthenelse ( cond , ifso , Some ifnot ) -> Lifthenelse ( transl_exp cond , event_before ifso ( transl_exp ifso ) , event_before ifnot ( transl_exp ifnot ) ) | Texp_ifthenelse ( cond , ifso , None ) -> Lifthenelse ( transl_exp cond , event_before ifso ( transl_exp ifso ) , lambda_unit ) | Texp_sequence ( expr1 , expr2 ) -> Lsequence ( transl_exp expr1 , event_before expr2 ( transl_exp expr2 ) ) | Texp_while ( cond , body ) -> Lwhile ( transl_exp cond , event_before body ( transl_exp body ) ) | Texp_for ( param , _ , low , high , dir , body ) -> Lfor ( param , transl_exp low , transl_exp high , dir , event_before body ( transl_exp body ) ) | Texp_send ( _ , _ , Some exp ) -> transl_exp exp | Texp_send ( expr , met , None ) -> let obj = transl_exp expr in let lam = match met with Tmeth_val id -> Lsend ( Self , Lvar id , obj , [ ] , e . exp_loc ) | Tmeth_name nm -> let ( tag , cache ) = Translobj . meth obj nm in let kind = if cache = [ ] then Public else Cached in Lsend ( kind , tag , obj , cache , e . exp_loc ) in event_after e lam | Texp_new ( cl , { Location . loc = loc } , _ ) -> Lapply { ap_should_be_tailcall = false ; ap_loc = loc ; ap_func = Lprim ( Pfield 0 , [ transl_class_path loc e . exp_env cl ] , loc ) ; ap_args [ = lambda_unit ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } | Texp_instvar ( path_self , path , _ ) -> let self = transl_value_path e . exp_loc e . exp_env path_self in let var = transl_value_path e . exp_loc e . exp_env path in Lprim ( Pfield_computed , [ self ; var ] , e . exp_loc ) | Texp_setinstvar ( path_self , path , _ , expr ) -> let self = transl_value_path e . exp_loc e . exp_env path_self in let var = transl_value_path e . exp_loc e . exp_env path in transl_setinstvar e . exp_loc self var expr | Texp_override ( path_self , modifs ) -> let self = transl_value_path e . exp_loc e . exp_env path_self in let cpy = Ident . create_local " copy " in Llet ( Strict , Pgenval , cpy , Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = Translobj . oo_prim " copy " ; ap_args [ = self ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } , List . fold_right ( fun ( path , _ , expr ) rem -> let var = transl_value_path e . exp_loc e . exp_env path in Lsequence ( transl_setinstvar Location . none ( Lvar cpy ) var expr , rem ) ) modifs ( Lvar cpy ) ) | Texp_letmodule ( None , loc , Mp_present , modl , body ) -> let lam = ! transl_module Tcoerce_none None modl in Lsequence ( Lprim ( Pignore , [ lam ] , loc . loc ) , transl_exp body ) | Texp_letmodule ( Some id , loc , Mp_present , modl , body ) -> let defining_expr = Levent ( ! transl_module Tcoerce_none None modl , { lev_loc = loc . loc ; lev_kind = Lev_module_definition id ; lev_repr = None ; lev_env = Env . empty ; } ) in Llet ( Strict , Pgenval , id , defining_expr , transl_exp body ) | Texp_letmodule ( _ , _ , Mp_absent , _ , body ) -> transl_exp body | Texp_letexception ( cd , body ) -> Llet ( Strict , Pgenval , cd . ext_id , transl_extension_constructor e . exp_env None cd , transl_exp body ) | Texp_pack modl -> ! transl_module Tcoerce_none None modl | Texp_assert { exp_desc = Texp_construct ( _ , { cstr_name " = false " } , _ ) } -> assert_failed e | Texp_assert ( cond ) -> if ! Clflags . noassert then lambda_unit else Lifthenelse ( transl_exp cond , lambda_unit , assert_failed e ) | Texp_lazy e -> begin match Typeopt . classify_lazy_argument e with | ` Constant_or_function -> transl_exp e | ` Float_that_cannot_be_shortcut -> Lprim ( Pmakeblock ( Obj . forward_tag , Immutable , None ) , [ transl_exp e ] , e . exp_loc ) | ` Identifier ` Forward_value -> Lprim ( Popaque , [ Lprim ( Pmakeblock ( Obj . forward_tag , Immutable , None ) , [ transl_exp e ] , e . exp_loc ) ] , e . exp_loc ) | ` Identifier ` Other -> transl_exp e | ` Other -> let fn = Lfunction { kind = Curried ; params = [ Ident . create_local " param " , Pgenval ] ; return = Pgenval ; attr = default_function_attribute ; loc = e . exp_loc ; body = transl_exp e } in Lprim ( Pmakeblock ( Config . lazy_tag , Mutable , None ) , [ fn ] , e . exp_loc ) end | Texp_object ( cs , meths ) -> let cty = cs . cstr_type in let cl = Ident . create_local " class " in ! transl_object cl meths { cl_desc = Tcl_structure cs ; cl_loc = e . exp_loc ; cl_type = Cty_signature cty ; cl_env = e . exp_env ; cl_attributes = [ ] ; } | Texp_letop { let_ ; ands ; param ; body ; partial } -> event_after e ( transl_letop e . exp_loc e . exp_env let_ ands param body partial ) | Texp_unreachable -> raise ( Error ( e . exp_loc , Unreachable_reached ) ) | Texp_open ( od , e ) -> let pure = pure_module od . open_expr in begin match od . open_bound_items with | [ ] when pure = Alias -> transl_exp e | _ -> let oid = Ident . create_local " open " in let body , _ = List . fold_left ( fun ( body , pos ) id -> Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar oid ] , od . open_loc ) , body ) , pos + 1 ) ( transl_exp e , 0 ) ( bound_value_identifiers od . open_bound_items ) in Llet ( pure , Pgenval , oid , ! transl_module Tcoerce_none None od . open_expr , body ) end match m . mod_desc with Tmod_ident _ -> Alias | Tmod_constraint ( m , _ , _ , _ ) -> pure_module m | _ -> Strict List . map transl_exp expr_list let transl_with_shape e = let shape = Typeopt . value_kind e . exp_env e . exp_type in transl_exp e , shape in List . split ( List . map transl_with_shape expr_list ) let expr = event_before rhs ( transl_exp rhs ) in match guard with | None -> expr | Some cond -> event_before cond ( Lifthenelse ( transl_exp cond , expr , staticfail ) ) c_lhs , transl_guard c_guard c_rhs let cases = List . filter ( fun c -> c . c_rhs . exp_desc <> Texp_unreachable ) cases in List . map transl_case cases iter_exn_names Translprim . add_exception_ident c_lhs ; Misc . try_finally ( fun ( ) -> c_lhs , transl_guard c_guard c_rhs ) ~ always ( : fun ( ) -> iter_exn_names Translprim . remove_exception_ident c_lhs ) let cases = List . filter ( fun c -> c . c_rhs . exp_desc <> Texp_unreachable ) cases in List . map transl_case_try cases let patl_expr_list = List . filter ( fun ( _ , _ , e ) -> e . exp_desc <> Texp_unreachable ) patl_expr_list in List . map ( fun ( patl , guard , expr ) -> ( patl , transl_guard guard expr ) ) patl_expr_list ( ? specialised = Default_specialise ) lam sargs loc = let lapply funct args = match funct with Lsend ( k , lmet , lobj , largs , loc ) -> Lsend ( k , lmet , lobj , largs @ args , loc ) | Levent ( Lsend ( k , lmet , lobj , largs , loc ) , _ ) -> Lsend ( k , lmet , lobj , largs @ args , loc ) | Lapply ap -> Lapply { ap with ap_args = ap . ap_args @ args ; ap_loc = loc } | lexp -> Lapply { ap_should_be_tailcall = should_be_tailcall ; ap_loc = loc ; ap_func = lexp ; ap_args = args ; ap_inlined = inlined ; ap_specialised = specialised ; } in let rec build_apply lam args = function ( None , optional ) :: l -> let defs = ref [ ] in let protect name lam = match lam with Lvar _ | Lconst _ -> lam | _ -> let id = Ident . create_local name in defs := ( id , lam ) :: ! defs ; Lvar id in let args , args ' = if List . for_all ( fun ( _ , opt ) -> opt ) args then [ ] , args else args , [ ] in let lam = if args = [ ] then lam else lapply lam ( List . rev_map fst args ) in let handle = protect " func " lam in let l = List . map ( fun ( arg , opt ) -> Option . map ( protect " arg " ) arg , opt ) l in let id_arg = Ident . create_local " param " in let body = match build_apply handle ( ( Lvar id_arg , optional ) :: args ' ) l with Lfunction { kind = Curried ; params = ids ; return ; body = lam ; attr ; loc } -> Lfunction { kind = Curried ; params = ( id_arg , Pgenval ) :: ids ; return ; body = lam ; attr ; loc } | Levent ( Lfunction { kind = Curried ; params = ids ; return ; body = lam ; attr ; loc } , _ ) -> Lfunction { kind = Curried ; params = ( id_arg , Pgenval ) :: ids ; return ; body = lam ; attr ; loc } | lam -> Lfunction { kind = Curried ; params = [ id_arg , Pgenval ] ; return = Pgenval ; body = lam ; attr = default_stub_attribute ; loc = loc } in List . fold_left ( fun body ( id , lam ) -> Llet ( Strict , Pgenval , id , lam , body ) ) body ! defs | ( Some arg , optional ) :: l -> build_apply lam ( ( arg , optional ) :: args ) l | [ ] -> lapply lam ( List . rev_map fst args ) in ( build_apply lam [ ] ( List . map ( fun ( l , x ) -> Option . map transl_exp x , Btype . is_optional l ) sargs ) : Lambda . lambda ) match cases with [ { c_lhs = pat ; c_guard = None ; c_rhs { = exp_desc = Texp_function { arg_label = _ ; param = param ' ; cases ; partial = partial ' ; } ; exp_env ; exp_type } as exp } ] when Parmatch . inactive ~ partial pat -> let kind = value_kind pat . pat_env pat . pat_type in let return_kind = function_return_value_kind exp_env exp_type in let ( ( _ , params , return ) , body ) = transl_function exp . exp_loc return_kind false repr partial ' param ' cases in ( ( Curried , ( param , kind ) :: params , return ) , Matching . for_function loc None ( Lvar param ) [ pat , body ] partial ) | { c_lhs { = pat_desc = Tpat_tuple pl } } :: _ when untuplify_fn -> begin try let size = List . length pl in let pats_expr_list = List . map ( fun { c_lhs ; c_guard ; c_rhs } -> ( Matching . flatten_pattern size c_lhs , c_guard , c_rhs ) ) cases in let kinds = match pats_expr_list with | [ ] -> assert false | ( pats , _ , _ ) :: cases -> let first_case_kinds = List . map ( fun pat -> value_kind pat . pat_env pat . pat_type ) pats in List . fold_left ( fun kinds ( pats , _ , _ ) -> List . map2 ( fun kind pat -> value_kind_union kind ( value_kind pat . pat_env pat . pat_type ) ) kinds pats ) first_case_kinds cases in let tparams = List . map ( fun kind -> Ident . create_local " param " , kind ) kinds in let params = List . map fst tparams in ( ( Tupled , tparams , return ) , Matching . for_tupled_function loc params ( transl_tupled_cases pats_expr_list ) partial ) with Matching . Cannot_flatten -> ( ( Curried , [ param , Pgenval ] , return ) , Matching . for_function loc repr ( Lvar param ) ( transl_cases cases ) partial ) end | { c_lhs = pat } :: other_cases -> let kind = List . fold_left ( fun k { c_lhs = pat } -> Typeopt . value_kind_union k ( value_kind pat . pat_env pat . pat_type ) ) ( value_kind pat . pat_env pat . pat_type ) other_cases in ( ( Curried , [ param , kind ] , return ) , Matching . for_function loc repr ( Lvar param ) ( transl_cases cases ) partial ) | [ ] -> ( ( Curried , [ param , Pgenval ] , return ) , Matching . for_function loc repr ( Lvar param ) ( transl_cases cases ) partial ) match rec_flag with Nonrecursive -> let rec transl = function [ ] -> fun body -> body | { vb_pat = pat ; vb_expr = expr ; vb_attributes = attr ; vb_loc } :: rem -> let lam = transl_exp expr in let lam = Translattribute . add_function_attributes lam vb_loc attr in let mk_body = transl rem in fun body -> Matching . for_let pat . pat_loc lam pat ( mk_body body ) in transl pat_expr_list | Recursive -> let idlist = List . map ( fun { vb_pat = pat } -> match pat . pat_desc with Tpat_var ( id , _ ) -> id | Tpat_alias ( { pat_desc = Tpat_any } , id , _ ) -> id | _ -> assert false ) pat_expr_list in let transl_case { vb_expr = expr ; vb_attributes ; vb_loc } id = let lam = transl_exp expr in let lam = Translattribute . add_function_attributes lam vb_loc vb_attributes in ( id , lam ) in let lam_bds = List . map2 transl_case pat_expr_list idlist in fun body -> Lletrec ( lam_bds , body ) Lprim ( Psetfield_computed ( maybe_pointer expr , Assignment ) , [ self ; var ; transl_exp expr ] , loc ) let size = Array . length fields in let no_init = match opt_init_expr with None -> true | _ -> false in if no_init || size < Config . max_young_wosize then begin let init_id = Ident . create_local " init " in let lv = Array . mapi ( fun i ( _ , definition ) -> match definition with | Kept typ -> let field_kind = value_kind env typ in let access = match repres with Record_regular | Record_inlined _ -> Pfield i | Record_unboxed _ -> assert false | Record_extension _ -> Pfield ( i + 1 ) | Record_float -> Pfloatfield i in Lprim ( access , [ Lvar init_id ] , loc ) , field_kind | Overridden ( _lid , expr ) -> let field_kind = value_kind expr . exp_env expr . exp_type in transl_exp expr , field_kind ) fields in let ll , shape = List . split ( Array . to_list lv ) in let mut = if Array . exists ( fun ( lbl , _ ) -> lbl . lbl_mut = Mutable ) fields then Mutable else Immutable in let lam = try if mut = Mutable then raise Not_constant ; let cl = List . map extract_constant ll in match repres with | Record_regular -> Lconst ( Const_block ( 0 , cl ) ) | Record_inlined tag -> Lconst ( Const_block ( tag , cl ) ) | Record_unboxed _ -> Lconst ( match cl with [ v ] -> v | _ -> assert false ) | Record_float -> Lconst ( Const_float_array ( List . map extract_float cl ) ) | Record_extension _ -> raise Not_constant with Not_constant -> match repres with Record_regular -> Lprim ( Pmakeblock ( 0 , mut , Some shape ) , ll , loc ) | Record_inlined tag -> Lprim ( Pmakeblock ( tag , mut , Some shape ) , ll , loc ) | Record_unboxed _ -> ( match ll with [ v ] -> v | _ -> assert false ) | Record_float -> Lprim ( Pmakearray ( Pfloatarray , mut ) , ll , loc ) | Record_extension path -> let slot = transl_extension_path loc env path in Lprim ( Pmakeblock ( 0 , mut , Some ( Pgenval :: shape ) ) , slot :: ll , loc ) in begin match opt_init_expr with None -> lam | Some init_expr -> Llet ( Strict , Pgenval , init_id , transl_exp init_expr , lam ) end end else begin let copy_id = Ident . create_local " newrecord " in let update_field cont ( lbl , definition ) = match definition with | Kept _type -> cont | Overridden ( _lid , expr ) -> let upd = match repres with Record_regular | Record_inlined _ -> Psetfield ( lbl . lbl_pos , maybe_pointer expr , Assignment ) | Record_unboxed _ -> assert false | Record_float -> Psetfloatfield ( lbl . lbl_pos , Assignment ) | Record_extension _ -> Psetfield ( lbl . lbl_pos + 1 , maybe_pointer expr , Assignment ) in Lsequence ( Lprim ( upd , [ Lvar copy_id ; transl_exp expr ] , loc ) , cont ) in begin match opt_init_expr with None -> assert false | Some init_expr -> Llet ( Strict , Pgenval , copy_id , Lprim ( Pduprecord ( repres , size ) , [ transl_exp init_expr ] , loc ) , Array . fold_left update_field ( Lvar copy_id ) fields ) end end let rewrite_case ( val_cases , exn_cases , static_handlers as acc ) ( { c_lhs ; c_guard ; c_rhs } as case ) = if c_rhs . exp_desc = Texp_unreachable then acc else let val_pat , exn_pat = split_pattern c_lhs in match val_pat , exn_pat with | None , None -> assert false | Some pv , None -> let val_case = transl_case { case with c_lhs = pv } in val_case :: val_cases , exn_cases , static_handlers | None , Some pe -> let exn_case = transl_case_try { case with c_lhs = pe } in val_cases , exn_case :: exn_cases , static_handlers | Some pv , Some pe -> assert ( c_guard = None ) ; let lbl = next_raise_count ( ) in let static_raise ids = Lstaticraise ( lbl , List . map ( fun id -> Lvar id ) ids ) in let ids_full = Typedtree . pat_bound_idents_full pv in let ids = List . map ( fun ( id , _ , _ ) -> id ) ids_full in let ids_kinds = List . map ( fun ( id , _ , ty ) -> id , Typeopt . value_kind pv . pat_env ty ) ids_full in let vids = List . map Ident . rename ids in let pv = alpha_pat ( List . combine ids vids ) pv in iter_exn_names Translprim . add_exception_ident pe ; let rhs = Misc . try_finally ( fun ( ) -> event_before c_rhs ( transl_exp c_rhs ) ) ~ always ( : fun ( ) -> iter_exn_names Translprim . remove_exception_ident pe ) in ( pv , static_raise vids ) :: val_cases , ( pe , static_raise ids ) :: exn_cases , ( lbl , ids_kinds , rhs ) :: static_handlers in let val_cases , exn_cases , static_handlers = let x , y , z = List . fold_left rewrite_case ( [ ] , [ ] , [ ] ) pat_expr_list in List . rev x , List . rev y , List . rev z in let static_catch body val_ids handler = let id = Typecore . name_pattern " exn " ( List . map fst exn_cases ) in let static_exception_id = next_raise_count ( ) in Lstaticcatch ( Ltrywith ( Lstaticraise ( static_exception_id , body ) , id , Matching . for_trywith ( Lvar id ) exn_cases ) , ( static_exception_id , val_ids ) , handler ) in let classic = match arg , exn_cases with | { exp_desc = Texp_tuple argl } , [ ] -> assert ( static_handlers = [ ] ) ; Matching . for_multiple_match e . exp_loc ( transl_list argl ) val_cases partial | { exp_desc = Texp_tuple argl } , _ :: _ -> let val_ids = List . map ( fun arg -> Typecore . name_pattern " val " [ ] , Typeopt . value_kind arg . exp_env arg . exp_type ) argl in let lvars = List . map ( fun ( id , _ ) -> Lvar id ) val_ids in static_catch ( transl_list argl ) val_ids ( Matching . for_multiple_match e . exp_loc lvars val_cases partial ) | arg , [ ] -> assert ( static_handlers = [ ] ) ; Matching . for_function e . exp_loc None ( transl_exp arg ) val_cases partial | arg , _ :: _ -> let val_id = Typecore . name_cases " val " pat_expr_list in let k = Typeopt . value_kind arg . exp_env arg . exp_type in static_catch [ transl_exp arg ] [ val_id , k ] ( Matching . for_function e . exp_loc None ( Lvar val_id ) val_cases partial ) in List . fold_left ( fun body ( static_exception_id , val_ids , handler ) -> Lstaticcatch ( body , ( static_exception_id , val_ids ) , handler ) ) classic static_handlers let rec loop prev_lam = function | [ ] -> prev_lam | and_ :: rest -> let left_id = Ident . create_local " left " in let right_id = Ident . create_local " right " in let op = transl_ident and_ . bop_op_name . loc env and_ . bop_op_type and_ . bop_op_path and_ . bop_op_val in let exp = transl_exp and_ . bop_exp in let lam = bind Strict right_id exp ( Lapply { ap_should_be_tailcall = false ; ap_loc = and_ . bop_loc ; ap_func = op ; ap_args [ = Lvar left_id ; Lvar right_id ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } ) in bind Strict left_id prev_lam ( loop lam rest ) in let op = transl_ident let_ . bop_op_name . loc env let_ . bop_op_type let_ . bop_op_path let_ . bop_op_val in let exp = loop ( transl_exp let_ . bop_exp ) ands in let func = let return_kind = value_kind case . c_rhs . exp_env case . c_rhs . exp_type in let ( kind , params , return ) , body = event_function case . c_rhs ( function repr -> transl_function case . c_rhs . exp_loc return_kind ! Clflags . native_code repr partial param [ case ] ) in let attr = default_function_attribute in let loc = case . c_rhs . exp_loc in Lfunction { kind ; params ; return ; body ; attr ; loc } in Lapply { ap_should_be_tailcall = false ; ap_loc = loc ; ap_func = op ; ap_args [ = exp ; func ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise }
|
let report_error ppf = function | Free_super_var -> fprintf ppf " Ancestor names can only be used to select inherited methods " | Unreachable_reached -> fprintf ppf " Unreachable expression was reached "
|
let ( ) = Location . register_error_of_exn ( function | Error ( loc , err ) -> Some ( Location . error_of_printer ~ loc report_error err ) | _ -> None )
|
type unsafe_component = | Unsafe_module_binding | Unsafe_functor | Unsafe_non_function | Unsafe_typext
|
type unsafe_info = | Unsafe of { reason : unsafe_component ; loc : Location . t ; subid : Ident . t } | Unnamed
|
type error = Circular_dependency of ( Ident . t * unsafe_info ) list
|
let cons_opt x_opt xs = match x_opt with | None -> xs | Some x -> x :: xs
|
let global_path glob = Some ( Pident glob )
|
let functor_path path param = match path with None -> None | Some p -> Some ( Papply ( p , Pident param ) )
|
let field_path path field = match path with None -> None | Some p -> Some ( Pdot ( p , Ident . name field ) )
|
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 ) , List . map ( apply_coercion_field loc get_field ) pos_cc_list , loc ) in wrap_id_pos_list loc id_pos_list get_field lam ) | Tcoerce_functor ( cc_arg , cc_res ) -> let param = Ident . create_local " funarg " in let carg = apply_coercion loc Alias cc_arg ( Lvar param ) in apply_coercion_result loc strict arg [ param , Pgenval ] [ carg ] cc_res | 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_path loc env path in name_lambda strict arg ( fun _ -> apply_coercion loc Alias cc lam ) apply_coercion loc Alias cc ( get_field pos ) match cc_res with | Tcoerce_functor ( cc_arg , cc_res ) -> let param = Ident . create_local " funarg " in let arg = apply_coercion loc Alias cc_arg ( Lvar param ) in apply_coercion_result loc strict funct ( ( param , Pgenval ) :: params ) ( arg :: args ) cc_res | _ -> name_lambda strict funct ( fun id -> Lfunction { kind = Curried ; params = List . rev params ; return = Pgenval ; attr = { default_function_attribute with is_a_functor = true ; stub = true ; } ; loc = loc ; body = apply_coercion loc Strict cc_res ( Lapply { ap_should_be_tailcall = false ; ap_loc = loc ; ap_func = Lvar id ; ap_args = List . rev args ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } ) } ) let fv = free_variables lam in let ( lam , s ) = List . fold_left ( fun ( lam , s ) ( id ' , pos , c ) -> if Ident . Set . mem id ' fv then let id ' ' = Ident . create_local ( Ident . name id ' ) in ( Llet ( Alias , Pgenval , id ' ' , apply_coercion loc Alias c ( get_field pos ) , lam ) , Ident . Map . add id ' id ' ' s ) else ( lam , s ) ) ( lam , Ident . Map . empty ) id_pos_list in if s == Ident . Map . empty then lam else Lambda . rename s lam
|
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 , pos2 , compose_coercions c1 c2 ) ) ids1 in Tcoerce_structure ( List . map ( fun pc -> match pc with | _ , ( Tcoerce_primitive _ | Tcoerce_alias _ ) -> pc | ( p1 , c1 ) -> let ( p2 , c2 ) = v2 . ( p1 ) in ( p2 , compose_coercions c1 c2 ) ) pc1 , ids1 @ ids2 ) | ( Tcoerce_functor ( arg1 , res1 ) , Tcoerce_functor ( arg2 , res2 ) ) -> Tcoerce_functor ( compose_coercions arg2 arg1 , compose_coercions res1 res2 ) | ( c1 , Tcoerce_alias ( env , path , c2 ) ) -> Tcoerce_alias ( env , path , compose_coercions c1 c2 ) | ( _ , _ ) -> fatal_error " Translmod . compose_coercions "
|
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_functor _ -> raise ( Initialization_failure ( Unsafe { reason = Unsafe_functor ; loc ; subid } ) ) and init_shape_struct env sg = match sg with [ ] -> [ ] | Sig_value ( subid , { val_kind = Val_reg ; val_type = ty ; val_loc = loc } , _ ) :: rem -> let init_v = match Ctype . expand_head env ty with { desc = Tarrow ( _ , _ , _ , _ ) } -> Const_pointer 0 | { desc = Tconstr ( p , _ , _ ) } when Path . same p Predef . path_lazy_t -> Const_pointer 1 | _ -> let not_a_function = Unsafe { reason = Unsafe_non_function ; loc ; subid } in raise ( Initialization_failure not_a_function ) in init_v :: init_shape_struct env rem | Sig_value ( _ , { val_kind = Val_prim _ } , _ ) :: rem -> init_shape_struct env rem | Sig_value _ :: _rem -> assert false | Sig_type ( id , tdecl , _ , _ ) :: rem -> init_shape_struct ( Env . add_type ~ check : false id tdecl env ) rem | Sig_typext ( subid , { ext_loc = loc } , _ , _ ) :: _ -> raise ( Initialization_failure ( Unsafe { reason = Unsafe_typext ; loc ; subid } ) ) | Sig_module ( id , Mp_present , md , _ , _ ) :: rem -> init_shape_mod id md . md_loc env md . md_type :: init_shape_struct ( Env . add_module_declaration ~ check : false id Mp_present md env ) rem | Sig_module ( id , Mp_absent , md , _ , _ ) :: rem -> init_shape_struct ( Env . add_module_declaration ~ check : false id Mp_absent md env ) rem | Sig_modtype ( id , minfo , _ ) :: rem -> init_shape_struct ( Env . add_modtype id minfo env ) rem | Sig_class _ :: rem -> Const_pointer 2 :: init_shape_struct env rem | Sig_class_type _ :: rem -> init_shape_struct env rem in try Ok ( undefined_location modl . mod_loc , Lconst ( init_shape_mod id modl . mod_loc modl . mod_env modl . mod_type ) ) with Initialization_failure reason -> Result . Error ( reason )
|
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 -> assert false | Inprogress Some i when i = stop -> info i :: l | Inprogress Some i -> collect stop ( info i :: l ) i in collect cycle_start [ ] cycle_start
|
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 ( List . map ( fun ( _ , _ , _ , rhs ) -> rhs ) bindings ) in let fv = Array . map Lambda . free_variables rhs in let num_bindings = Array . length id in let status = Array . make num_bindings Undefined in let res = ref [ ] in let is_unsafe i = match init . ( i ) with | Ok _ -> false | Result . Error _ -> true in let init_res i = match init . ( i ) with | Result . Error _ -> None | Ok ( a , b ) -> Some ( a , b ) in let rec emit_binding parent i = match status . ( i ) with Defined -> ( ) | Inprogress _ -> status . ( i ) <- Inprogress parent ; let cycle = extract_unsafe_cycle id status init i in raise ( Error ( loc . ( i ) , Circular_dependency cycle ) ) | Undefined -> if is_unsafe i then begin status . ( i ) <- Inprogress parent ; for j = 0 to num_bindings - 1 do match id . ( j ) with | Id id when Ident . Set . mem id fv . ( i ) -> emit_binding ( Some i ) j | _ -> ( ) done end ; res := ( id . ( i ) , init_res i , rhs . ( i ) ) :: ! res ; status . ( i ) <- Defined in for i = 0 to num_bindings - 1 do match status . ( i ) with Undefined -> emit_binding None i | Inprogress _ -> assert false | Defined -> ( ) done ; List . rev ! res
|
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 . none ; ap_func = mod_prim " init_mod " ; ap_args [ = loc ; shape ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } , bind_inits rem ) and bind_strict = function [ ] -> patch_forwards bindings | ( Ignore_loc loc , None , rhs ) :: rem -> Lsequence ( Lprim ( Pignore , [ rhs ] , loc ) , bind_strict rem ) | ( Id id , None , rhs ) :: rem -> Llet ( Strict , Pgenval , id , rhs , bind_strict rem ) | ( _id , Some _ , _rhs ) :: rem -> bind_strict rem and patch_forwards = function [ ] -> cont | ( Ignore_loc _ , _ , _rhs ) :: rem | ( _ , None , _rhs ) :: rem -> patch_forwards rem | ( Id id , Some ( _loc , shape ) , rhs ) :: rem -> Lsequence ( Lapply { ap_should_be_tailcall = false ; ap_loc = Location . none ; ap_func = mod_prim " update_mod " ; ap_args [ = shape ; Lvar id ; rhs ] ; ap_inlined = Default_inline ; ap_specialised = Default_specialise } , patch_forwards rem ) in bind_inits bindings
|
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 in ( id_or_ignore_loc , modl . mod_loc , shape , compile_rhs id modl loc ) ) bindings ) ) cont
|
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 let arg_coercion , res_coercion = match coercion with | Tcoerce_none -> Tcoerce_none , Tcoerce_none | Tcoerce_functor ( arg_coercion , res_coercion ) -> arg_coercion , res_coercion | _ -> fatal_error " Translmod . merge_functors : bad coercion " in let loc = mexp . mod_loc in let path , param = match param with | Unit -> None , Ident . create_local " " * | Named ( None , _ , _ ) -> let id = Ident . create_local " _ " in functor_path path id , id | Named ( Some id , _ , _ ) -> functor_path path id , id in let inline_attribute = merge_inline_attributes inline_attribute inline_attribute ' loc in merge body res_coercion path ( ( param , loc , arg_coercion ) :: acc ) inline_attribute | _ -> finished in merge mexp coercion root_path [ ] Default_inline
|
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 param ' = Ident . rename param in let arg = apply_coercion loc Alias arg_coercion ( Lvar param ' ) in let params = ( param ' , Pgenval ) :: params in let body = Llet ( Alias , Pgenval , param , arg , body ) in params , body ) ( [ ] , transl_module res_coercion body_path body ) functor_params_rev in Lfunction { kind = Curried ; params ; return = Pgenval ; attr = { inline = inline_attribute ; specialise = Default_specialise ; local = Default_local ; is_a_functor = true ; stub = false ; } ; loc ; body ; } List . iter ( Translattribute . check_attribute_on_module mexp ) mexp . mod_attributes ; let loc = mexp . mod_loc in match mexp . mod_desc with | Tmod_ident ( path , _ ) -> apply_coercion loc Strict cc ( transl_module_path loc mexp . mod_env path ) | Tmod_structure str -> fst ( transl_struct loc [ ] cc rootpath str ) | Tmod_functor _ -> oo_wrap mexp . mod_env true ( fun ( ) -> compile_functor mexp cc rootpath loc ) ( ) | Tmod_apply ( funct , arg , ccarg ) -> let inlined_attribute , funct = Translattribute . get_and_remove_inlined_attribute_on_module funct in oo_wrap mexp . mod_env true ( apply_coercion loc Strict cc ) ( Lapply { ap_should_be_tailcall = false ; ap_loc = loc ; ap_func = transl_module Tcoerce_none None funct ; ap_args [ = transl_module ccarg None arg ] ; ap_inlined = inlined_attribute ; ap_specialised = Default_specialise } ) | Tmod_constraint ( arg , _ , _ , ccarg ) -> transl_module ( compose_coercions cc ccarg ) rootpath arg | Tmod_unpack ( arg , _ ) -> apply_coercion loc Strict cc ( Translcore . transl_exp arg ) transl_structure loc fields cc rootpath str . str_final_env str . str_items [ ] -> let body , size = match cc with Tcoerce_none -> Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun id -> Lvar id ) ( List . rev fields ) , loc ) , List . length fields | Tcoerce_structure ( pos_cc_list , id_pos_list ) -> let v = Array . of_list ( List . rev fields ) in let get_field pos = if pos < 0 then lambda_unit else Lvar v . ( pos ) in let ids = List . fold_right Ident . Set . add fields Ident . Set . empty in let lam = Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun ( pos , cc ) -> match cc with Tcoerce_primitive p -> Translprim . transl_primitive p . pc_loc p . pc_desc p . pc_env p . pc_type None | _ -> apply_coercion loc Strict cc ( get_field pos ) ) pos_cc_list , loc ) and id_pos_list = List . filter ( fun ( id , _ , _ ) -> not ( Ident . Set . mem id ids ) ) id_pos_list in wrap_id_pos_list loc id_pos_list get_field lam , List . length pos_cc_list | _ -> fatal_error " Translmod . transl_structure " in ( if ! Clflags . debug && not ! Clflags . native_code then Levent ( body , { lev_loc = loc ; lev_kind = Lev_pseudo ; lev_repr = None ; lev_env = final_env } ) else body ) , size | item :: rem -> match item . str_desc with | Tstr_eval ( expr , _ ) -> let body , size = transl_structure loc fields cc rootpath final_env rem in Lsequence ( transl_exp expr , body ) , size | Tstr_value ( rec_flag , pat_expr_list ) -> let mk_lam_let = transl_let rec_flag pat_expr_list in let ext_fields = List . rev_append ( let_bound_idents pat_expr_list ) fields in let body , size = transl_structure loc ext_fields cc rootpath final_env rem in mk_lam_let body , size | Tstr_primitive descr -> record_primitive descr . val_val ; transl_structure loc fields cc rootpath final_env rem | Tstr_type _ -> transl_structure loc fields cc rootpath final_env rem | Tstr_typext ( tyext ) -> let ids = List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors in let body , size = transl_structure loc ( List . rev_append ids fields ) cc rootpath final_env rem in transl_type_extension item . str_env rootpath tyext body , size | Tstr_exception ext -> let id = ext . tyexn_constructor . ext_id in let path = field_path rootpath id in let body , size = transl_structure loc ( id :: fields ) cc rootpath final_env rem in Llet ( Strict , Pgenval , id , transl_extension_constructor item . str_env path ext . tyexn_constructor , body ) , size | Tstr_module ( { mb_presence = Mp_present } as mb ) -> let id = mb . mb_id in let module_body = transl_module Tcoerce_none ( Option . bind id ( field_path rootpath ) ) mb . mb_expr in let module_body = Translattribute . add_inline_attribute module_body mb . mb_loc mb . mb_attributes in let body , size = transl_structure loc ( cons_opt id fields ) cc rootpath final_env rem in begin match id with | None -> Lsequence ( Lprim ( Pignore , [ module_body ] , mb . mb_name . loc ) , body ) , size | Some id -> let module_body = Levent ( module_body , { lev_loc = mb . mb_loc ; lev_kind = Lev_module_definition id ; lev_repr = None ; lev_env = Env . empty ; } ) in Llet ( pure_module mb . mb_expr , Pgenval , id , module_body , body ) , size end | Tstr_module { mb_presence = Mp_absent } -> transl_structure loc fields cc rootpath final_env rem | Tstr_recmodule bindings -> let ext_fields = List . rev_append ( List . filter_map ( fun mb -> mb . mb_id ) bindings ) fields in let body , size = transl_structure loc ext_fields cc rootpath final_env rem in let lam = compile_recmodule ( fun id modl loc -> match id with | None -> transl_module Tcoerce_none None modl | Some id -> let module_body = transl_module Tcoerce_none ( field_path rootpath id ) modl in Levent ( module_body , { lev_loc = loc ; lev_kind = Lev_module_definition id ; lev_repr = None ; lev_env = Env . empty ; } ) ) bindings body in lam , size | Tstr_class cl_list -> let ( ids , class_bindings ) = transl_class_bindings cl_list in let body , size = transl_structure loc ( List . rev_append ids fields ) cc rootpath final_env rem in Lletrec ( class_bindings , body ) , size | Tstr_include incl -> let ids = bound_value_identifiers incl . incl_type in let modl = incl . incl_mod in let mid = Ident . create_local " include " in let rec rebind_idents pos newfields = function [ ] -> transl_structure loc newfields cc rootpath final_env rem | id :: ids -> let body , size = rebind_idents ( pos + 1 ) ( id :: newfields ) ids in Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , incl . incl_loc ) , body ) , size in let body , size = rebind_idents 0 fields ids in Llet ( pure_module modl , Pgenval , mid , transl_module Tcoerce_none None modl , body ) , size | Tstr_open od -> let pure = pure_module od . open_expr in begin match od . open_bound_items with | [ ] when pure = Alias -> transl_structure loc fields cc rootpath final_env rem | _ -> let ids = bound_value_identifiers od . open_bound_items in let mid = Ident . create_local " open " in let rec rebind_idents pos newfields = function [ ] -> transl_structure loc newfields cc rootpath final_env rem | id :: ids -> let body , size = rebind_idents ( pos + 1 ) ( id :: newfields ) ids in Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , od . open_loc ) , body ) , size in let body , size = rebind_idents 0 fields ids in Llet ( pure , Pgenval , mid , transl_module Tcoerce_none None od . open_expr , body ) , size end | Tstr_modtype _ | Tstr_class_type _ | Tstr_attribute _ -> transl_structure loc fields cc rootpath final_env rem
|
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 . Set . empty ) ( Translprim . get_used_primitives ( ) ) in let required = List . fold_right add_global ( Env . get_required_globals ( ) ) required in Env . reset_required_globals ( ) ; Translprim . clear_used_primitives ( ) ; required
|
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_path module_id ) str ) in { module_ident = module_id ; main_module_block_size = size ; required_globals = required_globals ~ flambda : true body ; code = body }
|
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 -> List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors @ defined_idents rem | Tstr_exception ext -> ext . tyexn_constructor . ext_id :: defined_idents rem | Tstr_module { mb_id = Some id ; mb_presence = Mp_present } -> id :: defined_idents rem | Tstr_module ( { mb_id = None } { | mb_presence = Mp_absent } ) -> defined_idents rem | Tstr_recmodule decls -> List . filter_map ( fun mb -> mb . mb_id ) decls @ defined_idents rem | Tstr_modtype _ -> defined_idents rem | Tstr_open od -> bound_value_identifiers od . open_bound_items @ defined_idents rem | Tstr_class cl_list -> List . map ( fun ( ci , _ ) -> ci . ci_id_class ) cl_list @ defined_idents rem | Tstr_class_type _ -> defined_idents rem | Tstr_include incl -> bound_value_identifiers incl . incl_type @ defined_idents rem | Tstr_attribute _ -> defined_idents rem
|
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 _ -> more_idents rem | Tstr_modtype _ -> more_idents rem | Tstr_open od -> let rest = more_idents rem in begin match od . open_expr . mod_desc with | Tmod_structure str -> all_idents str . str_items @ rest | _ -> rest end | Tstr_class _ -> more_idents rem | Tstr_class_type _ -> more_idents rem | Tstr_include { incl_mod { = mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> all_idents str . str_items @ more_idents rem | Tstr_include _ -> more_idents rem | Tstr_module { mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_structure str } } | Tstr_module { mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> all_idents str . str_items @ more_idents rem | Tstr_module _ -> more_idents rem | Tstr_attribute _ -> more_idents rem [ ] -> [ ] | item :: rem -> match item . str_desc with | Tstr_eval _ -> all_idents rem | Tstr_value ( _rec_flag , pat_expr_list ) -> let_bound_idents pat_expr_list @ all_idents rem | Tstr_primitive _ -> all_idents rem | Tstr_type _ -> all_idents rem | Tstr_typext tyext -> List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors @ all_idents rem | Tstr_exception ext -> ext . tyexn_constructor . ext_id :: all_idents rem | Tstr_recmodule decls -> List . filter_map ( fun mb -> mb . mb_id ) decls @ all_idents rem | Tstr_modtype _ -> all_idents rem | Tstr_open od -> let rest = all_idents rem in begin match od . open_expr . mod_desc with | Tmod_structure str -> bound_value_identifiers od . open_bound_items @ all_idents str . str_items @ rest | _ -> bound_value_identifiers od . open_bound_items @ rest end | Tstr_class cl_list -> List . map ( fun ( ci , _ ) -> ci . ci_id_class ) cl_list @ all_idents rem | Tstr_class_type _ -> all_idents rem | Tstr_include { incl_type ; incl_mod { = mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> bound_value_identifiers incl_type @ all_idents str . str_items @ all_idents rem | Tstr_include incl -> bound_value_identifiers incl . incl_type @ all_idents rem | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_structure str } } | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr = { mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } , _ , _ , _ ) } } -> id :: all_idents str . str_items @ all_idents rem | Tstr_module { mb_id = Some id ; mb_presence = Mp_present } -> id :: all_idents rem | Tstr_module ( { mb_id = None } | { mb_presence = Mp_absent } ) -> all_idents rem | Tstr_attribute _ -> all_idents rem
|
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_path loc env path in apply_coercion loc Alias cc lam | _ -> apply_coercion loc Strict cc ( Lvar ids . ( pos ) )
|
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 . subst no_env_update subst ( transl_exp expr ) , transl_store rootpath subst cont rem ) | Tstr_value ( rec_flag , pat_expr_list ) -> let ids = let_bound_idents pat_expr_list in let lam = transl_let rec_flag pat_expr_list ( store_idents Location . none ids ) in Lsequence ( Lambda . subst no_env_update subst lam , transl_store rootpath ( add_idents false ids subst ) cont rem ) | Tstr_primitive descr -> record_primitive descr . val_val ; transl_store rootpath subst cont rem | Tstr_type _ -> transl_store rootpath subst cont rem | Tstr_typext ( tyext ) -> let ids = List . map ( fun ext -> ext . ext_id ) tyext . tyext_constructors in let lam = transl_type_extension item . str_env rootpath tyext ( store_idents Location . none ids ) in Lsequence ( Lambda . subst no_env_update subst lam , transl_store rootpath ( add_idents false ids subst ) cont rem ) | Tstr_exception ext -> let id = ext . tyexn_constructor . ext_id in let path = field_path rootpath id in let lam = transl_extension_constructor item . str_env path ext . tyexn_constructor in Lsequence ( Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst lam , store_ident ext . tyexn_constructor . ext_loc id ) , transl_store rootpath ( add_ident false id subst ) cont rem ) | Tstr_module { mb_id = None ; mb_name ; mb_presence = Mp_present ; mb_expr = modl ; mb_loc = loc ; mb_attributes } -> let lam = Translattribute . add_inline_attribute ( transl_module Tcoerce_none None modl ) loc mb_attributes in Lsequence ( Lprim ( Pignore , [ lam ] , mb_name . loc ) , transl_store rootpath subst cont rem ) | Tstr_module { mb_id = Some id ; mb_loc = loc ; mb_presence = Mp_present ; mb_expr { = mod_desc = Tmod_structure str } as mexp ; mb_attributes } -> List . iter ( Translattribute . check_attribute_on_module mexp ) mb_attributes ; let lam = transl_store ( field_path rootpath id ) subst lambda_unit str . str_items in let subst = ! transl_store_subst in Lsequence ( lam , Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst ( Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map ( fun id -> Lvar id ) ( defined_idents str . str_items ) , loc ) ) , Lsequence ( store_ident loc id , transl_store rootpath ( add_ident true id subst ) cont rem ) ) ) | Tstr_module { mb_id = Some id ; mb_loc = loc ; mb_presence = Mp_present ; mb_expr = { mod_desc = Tmod_constraint ( { mod_desc = Tmod_structure str } as mexp , _ , _ , ( Tcoerce_structure ( map , _ ) as _cc ) ) } ; mb_attributes } -> List . iter ( Translattribute . check_attribute_on_module mexp ) mb_attributes ; let lam = transl_store ( field_path rootpath id ) subst lambda_unit str . str_items in let subst = ! transl_store_subst in let field = field_of_str loc str in Lsequence ( lam , Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst ( Lprim ( Pmakeblock ( 0 , Immutable , None ) , List . map field map , loc ) ) , Lsequence ( store_ident loc id , transl_store rootpath ( add_ident true id subst ) cont rem ) ) ) | Tstr_module { mb_id = Some id ; mb_presence = Mp_present ; mb_expr = modl ; mb_loc = loc ; mb_attributes } -> let lam = Translattribute . add_inline_attribute ( transl_module Tcoerce_none ( field_path rootpath id ) modl ) loc mb_attributes in Llet ( Strict , Pgenval , id , Lambda . subst no_env_update subst lam , Lsequence ( store_ident loc id , transl_store rootpath ( add_ident true id subst ) cont rem ) ) | Tstr_module { mb_presence = Mp_absent } -> transl_store rootpath subst cont rem | Tstr_recmodule bindings -> let ids = List . filter_map ( fun mb -> mb . mb_id ) bindings in compile_recmodule ( fun id modl _loc -> Lambda . subst no_env_update subst ( transl_module Tcoerce_none ( Option . bind id ( field_path rootpath ) ) modl ) ) bindings ( Lsequence ( store_idents Location . none ids , transl_store rootpath ( add_idents true ids subst ) cont rem ) ) | Tstr_class cl_list -> let ( ids , class_bindings ) = transl_class_bindings cl_list in let lam = Lletrec ( class_bindings , store_idents Location . none ids ) in Lsequence ( Lambda . subst no_env_update subst lam , transl_store rootpath ( add_idents false ids subst ) cont rem ) | Tstr_include { incl_loc = loc ; incl_mod = { mod_desc = Tmod_constraint ( ( { mod_desc = Tmod_structure str } as mexp ) , _ , _ , ( Tcoerce_structure ( map , _ ) ) ) } ; incl_attributes ; incl_type ; } -> List . iter ( Translattribute . check_attribute_on_module mexp ) incl_attributes ; let lam = transl_store None subst lambda_unit str . str_items in let subst = ! transl_store_subst in let field = field_of_str loc str in let ids0 = bound_value_identifiers incl_type in let rec loop ids args = match ids , args with | [ ] , [ ] -> transl_store rootpath ( add_idents true ids0 subst ) cont rem | id :: ids , arg :: args -> Llet ( Alias , Pgenval , id , Lambda . subst no_env_update subst ( field arg ) , Lsequence ( store_ident loc id , loop ids args ) ) | _ -> assert false in Lsequence ( lam , loop ids0 map ) | Tstr_include incl -> let ids = bound_value_identifiers incl . incl_type in let modl = incl . incl_mod in let mid = Ident . create_local " include " in let loc = incl . incl_loc in let rec store_idents pos = function | [ ] -> transl_store rootpath ( add_idents true ids subst ) cont rem | id :: idl -> Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , loc ) , Lsequence ( store_ident loc id , store_idents ( pos + 1 ) idl ) ) in Llet ( Strict , Pgenval , mid , Lambda . subst no_env_update subst ( transl_module Tcoerce_none None modl ) , store_idents 0 ids ) | Tstr_open od -> begin match od . open_expr . mod_desc with | Tmod_structure str -> let lam = transl_store rootpath subst lambda_unit str . str_items in let ids = Array . of_list ( defined_idents str . str_items ) in let ids0 = bound_value_identifiers od . open_bound_items in let subst = ! transl_store_subst in let rec store_idents pos = function | [ ] -> transl_store rootpath ( add_idents true ids0 subst ) cont rem | id :: idl -> Llet ( Alias , Pgenval , id , Lvar ids . ( pos ) , Lsequence ( store_ident od . open_loc id , store_idents ( pos + 1 ) idl ) ) in Lsequence ( lam , Lambda . subst no_env_update subst ( store_idents 0 ids0 ) ) | _ -> let pure = pure_module od . open_expr in match od . open_bound_items with | [ ] when pure = Alias -> transl_store rootpath subst cont rem | _ -> let ids = bound_value_identifiers od . open_bound_items in let mid = Ident . create_local " open " in let loc = od . open_loc in let rec store_idents pos = function [ ] -> transl_store rootpath ( add_idents true ids subst ) cont rem | id :: idl -> Llet ( Alias , Pgenval , id , Lprim ( Pfield pos , [ Lvar mid ] , loc ) , Lsequence ( store_ident loc id , store_idents ( pos + 1 ) idl ) ) in Llet ( pure , Pgenval , mid , Lambda . subst no_env_update subst ( transl_module Tcoerce_none None od . open_expr ) , store_idents 0 ids ) end | Tstr_modtype _ | Tstr_class_type _ | Tstr_attribute _ -> transl_store rootpath subst cont rem and store_ident loc id = try let ( pos , cc ) = Ident . find_same id map in let init_val = apply_coercion loc Alias cc ( Lvar id ) in Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , loc ) ; init_val ] , loc ) with Not_found -> fatal_error ( " Translmod . store_ident : " ^ Ident . unique_name id ) and store_idents loc idlist = make_sequence ( store_ident loc ) idlist and add_ident may_coerce id subst = try let ( pos , cc ) = Ident . find_same id map in match cc with Tcoerce_none -> Ident . Map . add id ( Lprim ( Pfield pos , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ] , Location . none ) ) subst | _ -> if may_coerce then subst else assert false with Not_found -> assert false and add_idents may_coerce idlist subst = List . fold_right ( add_ident may_coerce ) idlist subst and store_primitive ( pos , prim ) cont = Lsequence ( Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ; Translprim . transl_primitive Location . none prim . pc_desc prim . pc_env prim . pc_type None ] , Location . none ) , cont ) and store_alias ( pos , env , path , cc ) = let path_lam = transl_module_path Location . none env path in let init_val = apply_coercion Location . none Strict cc path_lam in Lprim ( Psetfield ( pos , Pointer , Root_initialization ) , [ Lprim ( Pgetglobal glob , [ ] , Location . none ) ; init_val ] , Location . none ) in let aliases = make_sequence store_alias aliases in List . fold_right store_primitive prims ( transl_store ( global_path glob ) ! transl_store_subst aliases str )
|
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 -> natural_map 0 Ident . empty [ ] [ ] idlist | Tcoerce_structure ( pos_cc_list , _id_pos_list ) -> let idarray = Array . of_list idlist in let rec export_map pos map prims aliases undef = function | [ ] -> natural_map pos map prims aliases undef | ( _source_pos , Tcoerce_primitive p ) :: rem -> export_map ( pos + 1 ) map ( ( pos , p ) :: prims ) aliases undef rem | ( _source_pos , Tcoerce_alias ( env , path , cc ) ) :: rem -> export_map ( pos + 1 ) map prims ( ( pos , env , path , cc ) :: aliases ) undef rem | ( source_pos , cc ) :: rem -> let id = idarray . ( source_pos ) in export_map ( pos + 1 ) ( Ident . add id ( pos , cc ) map ) prims aliases ( list_remove id undef ) rem in export_map 0 Ident . empty [ ] [ ] idlist pos_cc_list | _ -> fatal_error " Translmod . build_ident_map " in natural_map pos map prims aliases more_ids
|
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 str ) in let f = function | [ { str_desc = Tstr_eval ( expr , _attrs ) } ] when topl -> assert ( size = 0 ) ; Lambda . subst ( fun _ _ env -> env ) ! transl_store_subst ( transl_exp expr ) | str -> transl_store_structure module_id map prims aliases str in transl_store_label_init module_id size f str
|
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_persistent module_name ; required_globals = required_globals ~ flambda : true code }
|
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
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.