text
stringlengths
12
786k
let get_pat_args_lazy p rem = match p with | { pat_desc = Tpat_any } -> Patterns . omega :: rem | { pat_desc = Tpat_lazy arg } -> arg :: rem | _ -> assert false
let prim_obj_tag = Primitive . simple ~ name " : caml_obj_tag " ~ arity : 1 ~ alloc : false
let get_mod_field modname field = lazy ( let mod_ident = Ident . create_persistent modname in let env = Env . add_persistent_structure mod_ident Env . initial_safe_string in match Env . open_pers_signature modname env with | Error ` Not_found -> fatal_error ( " Module " ^ modname ^ " unavaila...
let code_force_lazy_block = get_mod_field " CamlinternalLazy " " force_lazy_block "
let code_force_lazy = get_mod_field " CamlinternalLazy " " force "
let lazy_forward_field = Lambda . Pfield ( 0 , Reads_vary )
let inline_lazy_force_cond arg pos loc = let idarg = Ident . create_local " lzarg " in let varg = Lvar idarg in let tag = Ident . create_local " tag " in let tag_var = Lvar tag in let force_fun = Lazy . force code_force_lazy_block in Llet ( Strict , Pgenval , idarg , arg , Llet ( Alias ,...
let inline_lazy_force_switch arg pos loc = let idarg = Ident . create_local " lzarg " in let varg = Lvar idarg in let force_fun = Lazy . force code_force_lazy_block in Llet ( Strict , Pgenval , idarg , arg , Lifthenelse ( Lprim ( Pisint , [ varg ] , loc ) , varg , Lswitch ( var...
let inline_lazy_force arg pos loc = if ! Clflags . afl_instrument then Lapply { ap_tailcall = Default_tailcall ; ap_loc = loc ; ap_func = Lazy . force code_force_lazy ; ap_args = [ arg ] ; ap_region_close = pos ; ap_mode = alloc_heap ; ap_inlined = Default_inlined ; ap_specialised = De...
let get_expr_args_lazy ~ scopes head ( arg , _mut ) rem = let loc = head_loc ~ scopes head in ( inline_lazy_force arg Rc_normal loc , Strict ) :: rem
let divide_lazy ~ scopes head ctx pm = divide_line ( Context . specialize head ) ( get_expr_args_lazy ~ scopes ) get_pat_args_lazy head ctx pm
let get_pat_args_tuple arity p rem = match p with | { pat_desc = Tpat_any } -> Patterns . omegas arity @ rem | { pat_desc = Tpat_tuple args } -> args @ rem | _ -> assert false
let get_expr_args_tuple ~ scopes head ( arg , _mut ) rem = let loc = head_loc ~ scopes head in let arity = Patterns . Head . arity head in let rec make_args pos = if pos >= arity then rem else ( Lprim ( Pfield ( pos , Reads_agree ) , [ arg ] , loc ) , Alias ) :: make_args ( ...
let divide_tuple ~ scopes head ctx pm = let arity = Patterns . Head . arity head in divide_line ( Context . specialize head ) ( get_expr_args_tuple ~ scopes ) ( get_pat_args_tuple arity ) head ctx pm
let record_matching_line num_fields lbl_pat_list = let patv = Array . make num_fields Patterns . omega in List . iter ( fun ( _ , lbl , pat ) -> patv . ( lbl . lbl_pos ) <- pat ) lbl_pat_list ; Array . to_list patv
let get_pat_args_record num_fields p rem = match p with | { pat_desc = Tpat_any } -> record_matching_line num_fields [ ] @ rem | { pat_desc = Tpat_record ( lbl_pat_list , _ ) } -> record_matching_line num_fields lbl_pat_list @ rem | _ -> assert false
let get_expr_args_record ~ scopes head ( arg , _mut ) rem = let loc = head_loc ~ scopes head in let all_labels = let open Patterns . Head in match head . pat_desc with | Record ( lbl :: _ ) -> lbl . lbl_all | Record [ ] | _ -> assert false in let rec make_args pos = if pos >= Array ....
let divide_record all_labels ~ scopes head ctx pm = let head = expand_record_head head in divide_line ( Context . specialize head ) ( get_expr_args_record ~ scopes ) ( get_pat_args_record ( Array . length all_labels ) ) head ctx pm
let get_key_array = function | { pat_desc = Tpat_array patl } -> List . length patl | _ -> assert false
let get_pat_args_array p rem = match p with | { pat_desc = Tpat_array patl } -> patl @ rem | _ -> assert false
let get_expr_args_array ~ scopes kind head ( arg , _mut ) rem = let len = let open Patterns . Head in match head . pat_desc with | Array len -> len | _ -> assert false in let loc = head_loc ~ scopes head in let rec make_args pos = if pos >= len then rem else ( Lprim ( Parrayrefu kind , [ ...
let divide_array ~ scopes kind ctx pm = divide ( get_expr_args_array ~ scopes kind ) ( = ) get_key_array get_pat_args_array ctx pm
let prim_string_notequal = Pccall ( Primitive . simple ~ name " : caml_string_notequal " ~ arity : 2 ~ alloc : false )
let prim_string_compare = Pccall ( Primitive . simple ~ name " : caml_string_compare " ~ arity : 2 ~ alloc : false )
let bind_sw arg k = match arg with | Lvar _ -> k arg | _ -> let id = Ident . create_local " switch " in Llet ( Strict , Pgenval , id , arg , k ( Lvar id ) )
let make_string_test_sequence loc kind arg sw d = let d , sw = match d with | None -> ( match sw with | ( _ , d ) :: sw -> ( d , sw ) | [ ] -> assert false ) | Some d -> ( d , sw ) in bind_sw arg ( fun arg -> List . fold_right ( fun ( str , lam ) k -> Lifthenelse ( ...
let rec split k xs = match xs with | [ ] -> assert false | x0 :: xs -> if k <= 1 then ( [ ] , x0 , xs ) else let xs , y0 , ys = split ( k - 2 ) xs in ( x0 :: xs , y0 , ys )
let zero_lam = Lconst ( Const_base ( Const_int 0 ) )
let tree_way_test loc kind arg lt eq gt = Lifthenelse ( Lprim ( Pintcomp Clt , [ arg ; zero_lam ] , loc ) , lt , Lifthenelse ( Lprim ( Pintcomp Clt , [ zero_lam ; arg ] , loc ) , gt , eq , kind ) , kind )
let rec do_make_string_test_tree loc kind arg sw delta d = let len = List . length sw in if len <= strings_test_threshold + delta then make_string_test_sequence loc kind arg sw d else let lt , ( s , act ) , gt = split len sw in bind_sw ( Lprim ( prim_string_compare , [ arg ; Lconst ( Const_...
let expand_stringswitch loc kind arg sw d = match d with | None -> bind_sw arg ( fun arg -> do_make_string_test_tree loc kind arg sw 0 None ) | Some e -> bind_sw arg ( fun arg -> make_catch kind e ( fun d -> do_make_string_test_tree loc kind arg sw 1 ( Some d ) ) )
let handle_shared kind = let hs = ref ( fun x -> x ) in let handle_shared act = match act with | Switch . Single act -> act | Switch . Shared act -> let i , h = make_catch_delayed kind act in let ohs = ! hs in ( hs := fun act -> h ( ohs act ) ) ; make_exit i in ( hs , handle_shared ...
let share_actions_tree kind sw d = let store = StoreExp . mk_store ( ) in let d = match d with | None -> None | Some d -> Some ( store . Switch . act_store_shared ( ) d ) in let sw = List . map ( fun ( cst , act ) -> ( cst , store . Switch . act_store ( ) act ) ) sw ...
let rec uniq_lambda_list sw = match sw with | [ ] | [ _ ] -> sw | ( ( c1 , _ ) as p1 ) :: ( ( c2 , _ ) :: sw2 as sw1 ) -> if const_compare c1 c2 = 0 then uniq_lambda_list ( p1 :: sw2 ) else p1 :: uniq_lambda_list sw1
let sort_lambda_list l = let l = List . stable_sort ( fun ( x , _ ) ( y , _ ) -> const_compare x y ) l in uniq_lambda_list l
let rec do_tests_fail value_kind loc fail tst arg = function | [ ] -> fail | ( c , act ) :: rem -> Lifthenelse ( Lprim ( tst , [ arg ; Lconst ( Const_base c ) ] , loc ) , do_tests_fail value_kind loc fail tst arg rem , act , value_kind )
let rec do_tests_nofail value_kind loc tst arg = function | [ ] -> fatal_error " Matching . do_tests_nofail " | [ ( _ , act ) ] -> act | ( c , act ) :: rem -> Lifthenelse ( Lprim ( tst , [ arg ; Lconst ( Const_base c ) ] , loc ) , do_tests_nofail value_kind loc t...
let make_test_sequence value_kind loc fail tst lt_tst arg const_lambda_list = let const_lambda_list = sort_lambda_list const_lambda_list in let hs , const_lambda_list , fail = share_actions_tree value_kind const_lambda_list fail in let rec make_test_sequence const_lambda_list = if List . length const_lambda_l...
module SArg = struct type primitive = Lambda . primitive let eqint = Pintcomp Ceq let neint = Pintcomp Cne let leint = Pintcomp Cle let ltint = Pintcomp Clt let geint = Pintcomp Cge let gtint = Pintcomp Cgt type act = Lambda . lambda type loc = Lambda . scoped_location type value_kind = Lambda . valu...
let share_actions_sw kind sw = let store = StoreExp . mk_store ( ) in let fail = match sw . sw_failaction with | None -> None | Some fail -> Some ( store . Switch . act_store_shared ( ) fail ) in let consts = List . map ( fun ( i , e ) -> ( i , store . Switch . act_store (...
let reintroduce_fail sw = match sw . sw_failaction with | None -> let t = Hashtbl . create 17 in let seen ( _ , l ) = match as_simple_exit l with | Some i -> let old = try Hashtbl . find t i with Not_found -> 0 in Hashtbl . replace t i ( old + 1 ) | None -> ( ) in List . iter see...
module Switcher = Switch . Make ( SArg )
let rec last def = function | [ ] -> def | [ ( x , _ ) ] -> x | _ :: rem -> last def rem
let get_edges low high l = match l with | [ ] -> ( low , high ) | ( x , _ ) :: _ -> ( x , last high l )
let as_interval_canfail fail low high l = let store = StoreExp . mk_store ( ) in let do_store _tag act = let i = store . act_store ( ) act in i in let rec nofail_rec cur_low cur_high cur_act = function | [ ] -> if cur_high = high then [ ( cur_low , cur_high , cur_act ) ] else [ ( ...
let as_interval_nofail l = let store = StoreExp . mk_store ( ) in let rec some_hole = function | [ ] | [ _ ] -> false | ( i , _ ) :: ( ( j , _ ) :: _ as rem ) -> j > i + 1 || some_hole rem in let rec i_rec cur_low cur_high cur_act = function | [ ] -> [ ( cur_lo...
let sort_int_lambda_list l = List . sort ( fun ( i1 , _ ) ( i2 , _ ) -> if i1 < i2 then - 1 else if i2 < i1 then 1 else 0 ) l
let as_interval fail low high l = let l = sort_int_lambda_list l in ( get_edges low high l , match fail with | None -> as_interval_nofail l | Some act -> as_interval_canfail act low high l )
let call_switcher kind loc fail arg low high int_lambda_list = let edges , ( cases , actions ) = as_interval fail low high int_lambda_list in Switcher . zyva loc kind edges arg cases actions
let rec list_as_pat = function | [ ] -> fatal_error " Matching . list_as_pat " | [ pat ] -> pat | pat :: rem -> { pat with pat_desc = Tpat_or ( pat , list_as_pat rem , None ) }
let complete_pats_constrs = function | constr :: _ as constrs -> let tag_of_constr constr = constr . pat_desc . cstr_tag in let pat_of_constr cstr = let open Patterns . Head in to_omega_pattern { constr with pat_desc = Construct cstr } in List . map pat_of_constr ( complete_constrs constr ( List...
let mk_failaction_neg partial ctx def = match partial with | Partial -> ( match Default_environment . pop def with | Some ( ( _ , idef ) , _ ) -> ( Some ( Lstaticraise ( idef , [ ] ) ) , Jumps . singleton idef ctx ) | None -> ( None , Jumps . empty ) ) | Total ...
let mk_failaction_pos partial seen ctx defs = if dbg then ( Format . eprintf " ** POS **\ n " ; Default_environment . pp defs ; ( ) ) ; let rec scan_def env to_test defs = match ( to_test , Default_environment . pop defs ) with | [ ] , _ | _ , None -> List . fold_left ( ...
let combine_constant value_kind loc arg cst partial ctx def ( const_lambda_list , total , _pats ) = let fail , local_jumps = mk_failaction_neg partial ctx def in let lambda1 = match cst with | Const_int _ -> let int_lambda_list = List . map ( function | Const_int n , l -> ( n , l ) | _ ...
let split_cases tag_lambda_list = let rec split_rec = function | [ ] -> ( [ ] , [ ] ) | ( cstr_tag , act ) :: rem -> ( let consts , nonconsts = split_rec rem in match cstr_tag with | Cstr_constant n -> ( ( n , act ) :: consts , nonconsts ) | Cstr_block n -> ( const...
let split_extension_cases tag_lambda_list = let rec split_rec = function | [ ] -> ( [ ] , [ ] ) | ( cstr_tag , act ) :: rem -> ( let consts , nonconsts = split_rec rem in match cstr_tag with | Cstr_extension ( path , true ) -> ( ( path , act ) :: consts , nonconsts...
let combine_constructor value_kind loc arg pat_env cstr partial ctx def ( descr_lambda_list , total1 , pats ) = let tag_lambda ( cstr , act ) = ( cstr . cstr_tag , act ) in match cstr . cstr_tag with | Cstr_extension _ -> let fail , local_jumps = mk_failaction_neg partial ctx def in let ...
let make_test_sequence_variant_constant value_kind fail arg int_lambda_list = let _ , ( cases , actions ) = as_interval fail min_int max_int int_lambda_list in Switcher . test_sequence value_kind arg cases actions
let call_switcher_variant_constant kind loc fail arg int_lambda_list = call_switcher kind loc fail arg min_int max_int int_lambda_list
let call_switcher_variant_constr value_kind loc fail arg int_lambda_list = let v = Ident . create_local " variant " in Llet ( Alias , Pgenval , v , Lprim ( nonconstant_variant_field 0 , [ arg ] , loc ) , call_switcher value_kind loc fail ( Lvar v ) min_int max_int int_lambda_list )
let combine_variant value_kind loc row arg partial ctx def ( tag_lambda_list , total1 , _pats ) = let row = Btype . row_repr row in let num_constr = ref 0 in if row . row_closed then List . iter ( fun ( _ , f ) -> match Btype . row_field_repr f with | Rabsent | Reither ( true , _ ...
let combine_array value_kind loc arg kind partial ctx def ( len_lambda_list , total1 , _pats ) = let fail , local_jumps = mk_failaction_neg partial ctx def in let lambda1 = let newvar = Ident . create_local " len " in let switch = call_switcher value_kind loc fail ( Lvar newvar ) 0 max_int le...
let rec event_branch repr lam = match ( lam , repr ) with | _ , None -> lam | Levent ( lam ' , ev ) , Some r -> incr r ; Levent ( lam ' , { lev_loc = ev . lev_loc ; lev_kind = ev . lev_kind ; lev_repr = repr ; lev_env = ev . lev_env } ) | Llet ( str , k , id ...
let compile_list compile_fun division = let rec c_rec totals = function | [ ] -> ( [ ] , Jumps . unions totals , [ ] ) | ( key , cell ) :: rem -> ( if Context . is_empty cell . ctx then c_rec totals rem else begin match compile_fun cell . ctx cell . pm with | exception Unus...
let compile_orhandlers value_kind compile_fun lambda1 total1 ctx to_catch = let rec do_rec r total_r = function | [ ] -> ( r , total_r ) | { provenance = mat ; exit = i ; vars ; pm } :: rem -> ( let ctx = Context . select_columns mat ctx in match compile_fun ctx pm with | exception Un...
let compile_test compile_fun partial divide combine ctx to_match = let division = divide ctx to_match in let c_div = compile_list compile_fun division . cells in match c_div with | [ ] , _ , _ -> ( match mk_failaction_neg partial ctx to_match . default with | None , _ -> raise Unused | Some l...
let rec approx_present v = function | Lconst _ -> false | Lstaticraise ( _ , args ) -> List . exists ( fun lam -> approx_present v lam ) args | Lprim ( _ , args , _ ) -> List . exists ( fun lam -> approx_present v lam ) args | Llet ( Alias , _k , _ , l1 , l2 ) -> a...
let rec lower_bind v arg lam = match lam with | Lifthenelse ( cond , ifso , ifnot , kind ) -> ( let pcond = approx_present v cond and pso = approx_present v ifso and pnot = approx_present v ifnot in match ( pcond , pso , pnot ) with | false , false , false -> lam | false , true , fa...
let bind_check str v arg lam = match ( str , arg ) with | _ , Lvar _ -> bind str v arg lam | Alias , _ -> lower_bind v arg lam | _ , _ -> bind str v arg lam
let comp_exit ctx m = match Default_environment . pop m . default with | Some ( ( _ , i ) , _ ) -> ( Lstaticraise ( i , [ ] ) , Jumps . singleton i ctx ) | None -> fatal_error " Matching . comp_exit "
let rec comp_match_handlers value_kind comp_fun partial ctx first_match next_matchs = match next_matchs with | [ ] -> comp_fun partial ctx first_match | rem -> ( let rec c_rec body total_body = function | [ ] -> ( body , total_body ) | ( i , pm ) :: rem -> ( let ctx_i , total_rem =...
let rec name_pattern default = function | ( ( pat , _ ) , _ ) :: rem -> ( match pat . pat_desc with | Tpat_var ( id , _ ) -> id | Tpat_alias ( _ , id , _ ) -> id | _ -> name_pattern default rem ) | _ -> Ident . create_local default
let arg_to_var arg cls = match arg with | Lvar v -> ( v , arg ) | _ -> let v = name_pattern " * match " * cls in ( v , Lvar v )
let rec compile_match ~ scopes value_kind repr partial ctx ( m : initial_clause pattern_matching ) = match m . cases with | ( [ ] , action ) :: rem -> if is_guarded action then let lambda , total = compile_match ~ scopes value_kind None partial ctx { m with cases = rem } in ( event_bran...
let is_lazy_pat p = match p . pat_desc with | Tpat_lazy _ -> true | Tpat_alias _ | Tpat_variant _ | Tpat_record _ | Tpat_tuple _ | Tpat_construct _ | Tpat_array _ | Tpat_or _ | Tpat_constant _ | Tpat_var _ | Tpat_any -> false
let has_lazy p = Typedtree . exists_pattern is_lazy_pat p
let is_record_with_mutable_field p = match p . pat_desc with | Tpat_record ( lps , _ ) -> List . exists ( fun ( _ , lbl , _ ) -> match lbl . Types . lbl_mut with | Mutable -> true | Immutable -> false ) lps | Tpat_alias _ | Tpat_variant _ | Tpat_lazy _ | Tpat_tuple _ | Tpat...
let has_mutable p = Typedtree . exists_pattern is_record_with_mutable_field p
let check_partial has_mutable has_lazy pat_act_list = function | Partial -> Partial | Total -> if pat_act_list = [ ] || List . exists ( fun ( pats , lam ) -> has_mutable pats && ( is_guarded lam || has_lazy pats ) ) pat_act_list then Partial else Total
let check_partial_list pats_act_list = check_partial ( List . exists has_mutable ) ( List . exists has_lazy ) pats_act_list
let check_partial pat_act_list = check_partial has_mutable has_lazy pat_act_list
type failer_kind = | Raise_match_failure | Reraise_noloc of lambda
let failure_handler ~ scopes loc ~ failer ( ) = match failer with | Reraise_noloc exn_lam -> Lprim ( Praise Raise_reraise , [ exn_lam ] , Scoped_location . Loc_unknown ) | Raise_match_failure -> let sloc = Scoped_location . of_location ~ scopes loc in let slot = transl_extension_path sloc ...
let check_total ~ scopes value_kind loc ~ failer total lambda i = if Jumps . is_empty total then lambda else Lstaticcatch ( lambda , ( i , [ ] ) , failure_handler ~ scopes loc ~ failer ( ) , value_kind )
let compile_matching ~ scopes value_kind loc ~ failer repr arg pat_act_list partial = let partial = check_partial pat_act_list partial in match partial with | Partial -> ( let raise_num = next_raise_count ( ) in let pm = { cases = List . map ( fun ( pat , act ) -> ( [ pat ] , act ) ...
let for_function ~ scopes kind loc repr param pat_act_list partial = compile_matching ~ scopes kind loc ~ failer : Raise_match_failure repr param pat_act_list partial
let for_trywith ~ scopes value_kind loc param pat_act_list = compile_matching ~ scopes value_kind loc ~ failer ( : Reraise_noloc param ) None param pat_act_list Partial
let simple_for_let ~ scopes value_kind loc param pat body = compile_matching ~ scopes value_kind loc ~ failer : Raise_match_failure None param [ ( pat , body ) ] Partial
let rec map_return f = function | Llet ( str , k , id , l1 , l2 ) -> Llet ( str , k , id , l1 , map_return f l2 ) | Lletrec ( l1 , l2 ) -> Lletrec ( l1 , map_return f l2 ) | Lifthenelse ( lcond , lthen , lelse , k ) -> Lifthenelse ( lcond , map_return f lthen , ...
let assign_pat ~ scopes value_kind opt nraise catch_ids loc pat lam = let rec collect acc pat lam = match ( pat . pat_desc , lam ) with | Tpat_tuple patl , Lprim ( Pmakeblock _ , lams , _ ) -> opt := true ; List . fold_left2 collect acc patl lams | Tpat_tuple patl , Lconst ( Const_blo...
let for_let ~ scopes loc param pat body_kind body = match pat . pat_desc with | Tpat_any -> Lsequence ( param , body ) | Tpat_var ( id , _ ) -> let k = Typeopt . value_kind pat . pat_env pat . pat_type in Llet ( Strict , k , id , param , body ) | _ -> let opt = ref false in l...
let for_tupled_function ~ scopes loc kind paraml pats_act_list partial = let partial = check_partial_list pats_act_list partial in let raise_num = next_raise_count ( ) in let omega_params = [ Patterns . omega_list paraml ] in let pm = { cases = pats_act_list ; args = List . map ( fun id -> ( ...
let flatten_pattern size p = match p . pat_desc with | Tpat_tuple args -> args | Tpat_any -> Patterns . omegas size | _ -> raise Cannot_flatten
let flatten_simple_pattern size ( p : Simple . pattern ) = match p . pat_desc with | ` Tuple args -> args | ` Any -> Patterns . omegas size | ` Array _ | ` Variant _ | ` Record _ | ` Lazy _ | ` Construct _ | ` Constant _ -> let msg = Format . fprintf Format . str_format...
let flatten_cases size cases = List . map ( function | ( p , [ ] ) , action -> ( match flatten_simple_pattern size p with | p :: ps -> ( ( p , ps ) , action ) | [ ] -> assert false ) | _ -> fatal_error " Matching . flatten_hc_cases " ) cases
let flatten_pm size args pm = { args ; cases = flatten_cases size pm . cases ; default = Default_environment . flatten size pm . default }
let flatten_handler size handler = { handler with provenance = flatten_matrix size handler . provenance }
type pm_flattened = | FPmOr of pattern pm_or_compiled | FPm of pattern Non_empty_row . t clause pattern_matching
let flatten_precompiled size args pmh = match pmh with | Pm pm -> FPm ( flatten_pm size args pm ) | PmOr { body = b ; handlers = hs ; or_matrix = m } -> FPmOr { body = flatten_pm size args b ; handlers = List . map ( flatten_handler size ) hs ; or_matrix = flatten_matrix size m } |...
let compile_flattened ~ scopes value_kind repr partial ctx pmh = match pmh with | FPm pm -> compile_match_nonempty value_kind ~ scopes repr partial ctx pm | FPmOr { body = b ; handlers = hs } -> let lam , total = compile_match_nonempty value_kind ~ scopes repr partial ctx b in compile_orhandlers valu...