text
stringlengths
12
786k
let move_to mem_key src tgt = let mvs = MemKey . fold ( fun { tag = tag ; equiv = m } r -> StateSetSet . fold ( fun s r -> try let t = StateSet . choose s in let src = TagMap . find tag ( get_map t src ) and tgt = TagMap . find tag ( get_map t tgt ) in if src <> tgt then begin if i...
let get_state st = let key = get_key st in try let num = StateMap . find key ! state_map in num , move_to key . kmem st ( Table . get state_table num ) with Not_found -> let num = ! next_state_num in incr next_state_num ; let st , mvs = create_new_state st in Table . emit state_table st ; st...
let map_on_all_states f old_res = let res = ref old_res in begin try while true do let ( st , i ) = Stack . pop todo in let r = f st in res := ( r , i ) :: ! res done with Stack . Empty -> ( ) end ; ! res
let goto_state st = if dfa_state_is_empty st then Backtrack , [ ] else let n , moves = get_state st in Goto n , moves
let add_tags_to_map gen tags m = Tags . fold ( fun tag m -> let m = TagMap . remove tag m in TagMap . add tag ( alloc_new_addr tag gen ) m ) tags m
let apply_transition gen r pri m = function | ToAction n , tags -> let on , ( opri , _ ) = r . final in if n < on || ( on = n && pri < opri ) then let m = add_tags_to_map gen tags m in { r with final = n , ( pri , m ) } else r | OnChars n , tags -> try let ( opri , _ ) ...
let apply_transitions gen r pri m ts = TransSet . fold ( fun t r -> apply_transition gen r pri m t ) ts r
let rec split_env gen follow pos m s = function | [ ] -> [ ] | ( s1 , st1 ) as p :: rem -> let here = Cset . inter s s1 in if Cset . is_empty here then p :: split_env gen follow pos m s rem else let rest = Cset . diff s here in let rem = if Cset . is_empty rest then rem else split_env ge...
let comp_shift gen chars follow st = MemMap . fold ( fun pos ( _ , m ) env -> split_env gen follow . ( pos ) pos m chars . ( pos ) env ) st [ Cset . all_chars_eof , dfa_state_empty ]
let reachs chars follow st = let gen = create_new_addr_gen ( ) in let env = comp_shift gen chars follow st in let env = List . map ( fun ( s , dfa_state ) -> s , goto_state dfa_state ) env in let shift = Cset . env_to_array env in shift
let get_tag_mem n env t = try TagMap . find t env . ( n ) with | Not_found -> assert false
let do_tag_actions n env m = let used , r = TagMap . fold ( fun t m ( used , r ) -> let a = get_tag_mem n env t in Ints . add a used , SetTag ( a , m ) :: r ) m ( Ints . empty , [ ] ) in let _ , r = TagMap . fold ( fun tag m ( used , r ) -> if not ( Ints . mem...
let translate_state shortest_match tags chars follow st = let ( n , ( _ , m ) ) = st . final in if MemMap . empty = st . others then Perform ( n , do_tag_actions n tags m ) else if shortest_match then begin if n = no_action then Shift ( No_remember , reachs chars follow st . others ) ...
let make_tag_entry id start act a r = match a with | Sum ( Mem m , 0 ) -> TagMap . add { id = id ; start = start ; action = act } m r | _ -> r
let extract_tags l = let envs = Array . make ( List . length l ) TagMap . empty in List . iter ( fun ( act , m , _ ) -> envs . ( act ) <- List . fold_right ( fun ( ( name , _ ) , v ) r -> match v with | Ident_char ( _ , t ) -> make_tag_entry name true act t r ...
let make_dfa lexdef = let ( chars , entry_list ) = encode_lexdef lexdef in let follow = followpos ( Array . length chars ) entry_list in reset_state ( ) ; let r_states = ref [ ] in let initial_states = List . map ( fun ( le , args , shortest ) -> let tags = extract_tags le . l...
let tags = [ " control " ; " define " ; " structure " ; " char " ; " infix " ; " label " ; " uident " ] [ " blue " ; " forestgreen " ; " purple " ; " gray40 " ; " indianred4 " ; " saddlebrown " ; " midnightblue " ]
let init_tags tw = List . iter2 tags colors ~ f : begin fun tag col -> Text . tag_configure tw ~ tag ~ foreground ( ` : Color col ) end ; Text . tag_configure tw ~ tag " : error " ~ foreground ` : Red ; Text . tag_configure tw ~ tag " : error " ~ relief ` : Raised ; Text ....
let tag ( ? start = tstart ) ( ? stop = tend ) tw = let tpos c = ( Text . index tw ~ index : start , [ ` Char c ] ) in let text = Text . get tw ~ start ~ stop in let buffer = Lexing . from_string text in Location . init buffer " " ; Location . input_name := " " ; L...
type position = { pos_fname : string ; pos_lnum : int ; pos_bol : int ; pos_cnum : int ; }
let dummy_pos = { pos_fname = " " ; pos_lnum = 0 ; pos_bol = 0 ; pos_cnum = - 1 ; }
type lexbuf = { refill_buff : lexbuf -> unit ; mutable lex_buffer : bytes ; mutable lex_buffer_len : int ; mutable lex_abs_pos : int ; mutable lex_start_pos : int ; mutable lex_curr_pos : int ; mutable lex_last_pos : int ; mutable lex_last_action : int ; mutable lex_eof_reached : bool ; m...
type lex_tables = { lex_base : string ; lex_backtrk : string ; lex_default : string ; lex_trans : string ; lex_check : string ; lex_base_code : string ; lex_backtrk_code : string ; lex_default_code : string ; lex_trans_code : string ; lex_check_code : string ; lex_code : string ; } ...
let engine tbl state buf = let result = c_engine tbl state buf in if result >= 0 && buf . lex_curr_p != dummy_pos then begin buf . lex_start_p <- buf . lex_curr_p ; buf . lex_curr_p <- { buf . lex_curr_p with pos_cnum = buf . lex_abs_pos + buf . lex_curr_pos } ; end ; result
let new_engine tbl state buf = let result = c_new_engine tbl state buf in if result >= 0 && buf . lex_curr_p != dummy_pos then begin buf . lex_start_p <- buf . lex_curr_p ; buf . lex_curr_p <- { buf . lex_curr_p with pos_cnum = buf . lex_abs_pos + buf . lex_curr_pos } ; end ; result
let lex_refill read_fun aux_buffer lexbuf = let read = read_fun aux_buffer ( Bytes . length aux_buffer ) in let n = if read > 0 then read else ( lexbuf . lex_eof_reached <- true ; 0 ) in if lexbuf . lex_buffer_len + n > Bytes . length lexbuf . lex_buffer then begin if lexbuf . lex_buffer_...
let zero_pos = { pos_fname = " " ; pos_lnum = 1 ; pos_bol = 0 ; pos_cnum = 0 ; }
let from_function ( ? with_positions = true ) f = { refill_buff = lex_refill f ( Bytes . create 512 ) ; lex_buffer = Bytes . create 1024 ; lex_buffer_len = 0 ; lex_abs_pos = 0 ; lex_start_pos = 0 ; lex_curr_pos = 0 ; lex_last_pos = 0 ; lex_last_action = 0 ; lex_mem = [ ...
let from_channel ? with_positions ic = from_function ? with_positions ( fun buf n -> input ic buf 0 n )
let from_string ( ? with_positions = true ) s = { refill_buff = ( fun lexbuf -> lexbuf . lex_eof_reached <- true ) ; lex_buffer = Bytes . of_string s ; lex_buffer_len = String . length s ; lex_abs_pos = 0 ; lex_start_pos = 0 ; lex_curr_pos = 0 ; lex_last_pos = 0 ; lex_last_...
let set_position lexbuf position = lexbuf . lex_curr_p <- { position with pos_fname = lexbuf . lex_curr_p . pos_fname } ; lexbuf . lex_abs_pos <- position . pos_cnum
let set_filename lexbuf fname = lexbuf . lex_curr_p <- { lexbuf . lex_curr_p with pos_fname = fname }
let with_positions lexbuf = lexbuf . lex_curr_p != dummy_pos
let lexeme lexbuf = let len = lexbuf . lex_curr_pos - lexbuf . lex_start_pos in Bytes . sub_string lexbuf . lex_buffer lexbuf . lex_start_pos len
let sub_lexeme lexbuf i1 i2 = let len = i2 - i1 in Bytes . sub_string lexbuf . lex_buffer i1 len
let sub_lexeme_opt lexbuf i1 i2 = if i1 >= 0 then begin let len = i2 - i1 in Some ( Bytes . sub_string lexbuf . lex_buffer i1 len ) end else begin None end
let sub_lexeme_char lexbuf i = Bytes . get lexbuf . lex_buffer i
let sub_lexeme_char_opt lexbuf i = if i >= 0 then Some ( Bytes . get lexbuf . lex_buffer i ) else None
let lexeme_char lexbuf i = Bytes . get lexbuf . lex_buffer ( lexbuf . lex_start_pos + i )
let lexeme_start lexbuf = lexbuf . lex_start_p . pos_cnum
let lexeme_end lexbuf = lexbuf . lex_curr_p . pos_cnum
let lexeme_start_p lexbuf = lexbuf . lex_start_p
let lexeme_end_p lexbuf = lexbuf . lex_curr_p
let new_line lexbuf = let lcp = lexbuf . lex_curr_p in if lcp != dummy_pos then lexbuf . lex_curr_p <- { lcp with pos_lnum = lcp . pos_lnum + 1 ; pos_bol = lcp . pos_cnum ; }
let flush_input lb = lb . lex_curr_pos <- 0 ; lb . lex_abs_pos <- 0 ; let lcp = lb . lex_curr_p in if lcp != dummy_pos then lb . lex_curr_p <- { zero_pos with pos_fname = lcp . pos_fname } ; lb . lex_buffer_len <- 0 ;
type ' a ref = { mutable contents : ' a }
type ' a option = None | Some of ' a
module Definition = struct type descr = | Code of Code_id . t | Set_of_closures of { denv : Downwards_env . t ; closure_symbols_with_types : ( Symbol . t * Flambda2_types . t ) Function_slot . Lmap . t ; symbol_projections : Symbol_projection . t Variable . Map . t } | Block_like ...
type t = { definitions : Definition . t list ; bound_static : Bound_static . t ; defining_exprs : Rebuilt_static_const . Group . t ; symbol_projections : Symbol_projection . t Variable . Map . t ; is_fully_static : bool }
let definitions t = t . definitions
let symbol_projections t = t . symbol_projections
let free_names_of_defining_exprs t = Rebuilt_static_const . Group . free_names t . defining_exprs
let is_fully_static t = t . is_fully_static
let [ @ ocamlformat " disable " ] print ppf { definitions ; bound_static = _ ; defining_exprs = _ ; is_fully_static = _ ; symbol_projections = _ ; } = Format . fprintf ppf " [ @< hov 1 ( >% a ) ] " @ ( Format . pp_print_list ~ pp_sep : Format . pp_print_space De...
let compute_bound_static definitions = ListLabels . map definitions ~ f : Definition . bound_static_pattern |> Bound_static . create
let compute_defining_exprs definitions = ListLabels . map definitions ~ f : Definition . defining_expr |> Rebuilt_static_const . Group . create
let create_block_like symbol ~ symbol_projections defining_expr denv ty = if not ( Rebuilt_static_const . is_block defining_expr ) then Misc . fatal_errorf " Defining expression must be a block :@ % a " Rebuilt_static_const . print defining_expr ; let definition = Definition . block_like denv sym...
let create_set_of_closures denv ~ closure_symbols_with_types ~ symbol_projections defining_expr = if not ( Rebuilt_static_const . is_set_of_closures defining_expr ) then Misc . fatal_errorf " Defining expression must be a set of closures :@ % a " Rebuilt_static_const . print defining_expr ; let def...
let create_code code_id defining_expr = if not ( Rebuilt_static_const . is_code defining_expr ) then Misc . fatal_errorf " Defining expression must be code :@ % a " Rebuilt_static_const . print defining_expr ; let definition = Definition . code code_id defining_expr in let definitions = [ defin...
let create_definition definition = let definitions = [ definition ] in { definitions ; bound_static = compute_bound_static definitions ; defining_exprs = compute_defining_exprs definitions ; is_fully_static = Rebuilt_static_const . is_fully_static ( Definition . defining_expr definition ) ; s...
let concat ts = let definitions = List . fold_left ( fun definitions t -> t . definitions @ definitions ) [ ] ts in let bound_static = List . fold_left ( fun bound_static t -> Bound_static . concat t . bound_static bound_static ) Bound_static . empty ts in let defining_exprs = List . fo...
let defining_exprs t = Rebuilt_static_const . Group . create ( List . map Definition . defining_expr t . definitions )
let bound_static t = Bound_static . create ( List . map Definition . bound_static_pattern t . definitions )
let types_of_symbols t = ListLabels . fold_left t . definitions ~ init : Symbol . Map . empty ~ f ( : fun types_of_symbols definition -> Symbol . Map . disjoint_union ( Definition . types_of_symbols definition ) types_of_symbols )
let all_defined_symbols t = Symbol . Map . keys ( types_of_symbols t )
let apply_projection t proj = let symbol = Symbol_projection . symbol proj in let matching_defining_exprs = ListLabels . filter_map t . definitions ~ f ( : fun definition -> if Definition . binds_symbol definition symbol then Some ( Definition . defining_expr definition ) else None ) in match ma...
type t = | Empty | Leaf of LC . t | Leaf_array of { innermost_first : LC . t array } | Union of { outer : t ; inner : t }
let to_list_outermost_first t = let rec to_list t acc = match t with | Empty -> acc | Leaf const -> const :: acc | Leaf_array { innermost_first } -> List . rev ( Array . to_list innermost_first ) @ acc | Union { inner ; outer } -> to_list outer ( to_list inner acc ) in to_list t [ ] ...
let [ @ ocamlformat " disable " ] print ppf t = Format . fprintf ppf " [ @< hov 1 ( > outermost_first @ % a ) ] " @ ( Format . pp_print_list ~ pp_sep : Format . pp_print_space LC . print ) ( to_list_outermost_first t )
let is_empty t = match t with Empty -> true | Leaf _ | Leaf_array _ | Union _ -> false
let singleton const = Leaf const
let singleton_sorted_array_of_constants ~ innermost_first = if Array . length innermost_first < 1 then empty else Leaf_array { innermost_first }
let singleton_list_of_constants_order_does_not_matter constants = singleton_sorted_array_of_constants ~ innermost_first ( : Array . of_list constants )
let union_ordered ~ innermost ~ outermost = match innermost , outermost with | Empty , _ -> outermost | _ , Empty -> innermost | inner , outer -> Union { inner ; outer }
let union t1 t2 = union_ordered ~ innermost : t1 ~ outermost : t2
let add_innermost t const = if is_empty t then Leaf const else Union { inner = Leaf const ; outer = t }
let add_outermost t const = if is_empty t then Leaf const else Union { outer = Leaf const ; inner = t }
let rec fold_outermost_first t ~ init ~ f = match t with | Empty -> init | Leaf const -> f init const | Leaf_array { innermost_first } -> let acc = ref init in for i = Array . length innermost_first - 1 downto 0 do acc := f ! acc innermost_first . ( i ) done ; ! acc | Union { inner ; ...
let rec fold_innermost_first t ~ init ~ f = match t with | Empty -> init | Leaf const -> f init const | Leaf_array { innermost_first } -> ArrayLabels . fold_left innermost_first ~ init ~ f | Union { inner ; outer } -> let init = fold_innermost_first inner ~ init ~ f in fold_innermost_first o...
let all_defined_symbols t = fold t ~ init : Symbol . Set . empty ~ f ( : fun symbols const -> LC . all_defined_symbols const |> Symbol . Set . union symbols )
let add_to_denv ? maybe_already_defined denv lifted = let maybe_already_defined = match maybe_already_defined with None -> false | Some ( ) -> true in let denv = fold lifted ~ init : denv ~ f ( : fun denv lifted_constant -> let types_of_symbols = LC . types_of_symbols lifted_constant in Symbol . ...
module SCC_lifted_constants = Strongly_connected_components . Make ( CIS )
let build_dep_graph t = fold t ~ init ( : CIS . Map . empty , CIS . Map . empty ) ~ f ( : fun ( dep_graph , code_id_or_symbol_to_const ) lifted_constant -> ListLabels . fold_left ( LC . definitions lifted_constant ) ~ init ( : dep_graph , code_id_or_symbol_to_const ) ~ f ( ...
let remove_values_not_in_domain ( m : CIS . Set . t CIS . Map . t ) = CIS . Map . map ( fun values -> CIS . Set . filter ( fun value -> CIS . Map . mem value m ) values ) m
let sort0 t = let lifted_constants_dep_graph , code_id_or_symbol_to_const = build_dep_graph t in let lifted_constants_dep_graph = remove_values_not_in_domain lifted_constants_dep_graph in let innermost_first = lifted_constants_dep_graph |> SCC_lifted_constants . connected_components_sorted_from_roots_to_leaf ...
let sort t = match t with Empty | Leaf _ -> t | Leaf_array _ | Union _ -> sort0 t
type lifter = Flambda . program -> Flambda . program
type def = | Immutable of Variable . t * Flambda . named Flambda . With_free_variables . t | Mutable of Mutable_variable . t * Variable . t * Lambda . value_kind
let rebuild_let ( defs : def list ) ( body : Flambda . t ) = let module W = Flambda . With_free_variables in List . fold_left ( fun body def -> match def with | Immutable ( var , def ) -> W . create_let_reusing_defining_expr var def body | Mutable ( var , initial_value , contents_k...
let rec extract_let_expr ( acc : def list ) ( let_expr : Flambda . let_expr ) : def list * Flambda . t Flambda . With_free_variables . t = let module W = Flambda . With_free_variables in let acc = match let_expr with | { var = v1 ; defining_expr = Expr ( Let let2 ) ; _ } -> let...
let rec lift_lets_expr ( expr : Flambda . t ) ~ toplevel : Flambda . t = let module W = Flambda . With_free_variables in match expr with | Let let_expr -> let defs , body = extract_let_expr [ ] let_expr in let rev_defs = List . rev_map ( lift_lets_def ~ toplevel ) defs in let body = lif...
module Sort_lets = Strongly_connected_components . Make ( Variable )
let rebuild_let_rec ( defs ( : Variable . t * Flambda . named ) list ) body = let map = Variable . Map . of_list defs in let graph = Variable . Map . map ( fun named -> Variable . Set . filter ( fun v -> Variable . Map . mem v map ) ( Flambda . free_variables_named named ) ...
let lift_let_rec program = Flambda_iterators . map_exprs_at_toplevel_of_program program ~ f ( : Flambda_iterators . map_expr ( fun expr -> match expr with | Let_rec ( defs , body ) -> rebuild_let_rec defs body | expr -> expr ) )
let lift_lets program = let program = lift_let_rec program in Flambda_iterators . map_exprs_at_toplevel_of_program program ~ f ( : lift_lets_expr ~ toplevel : false )
let lifting_helper exprs ~ evaluation_order ~ create_body ~ name = let vars , lets = List . fold_right ( fun ( flam : Flambda . t ) ( vars , lets ) -> match flam with | Var v -> v :: vars , lets | expr -> let v = Variable . create name ~ current_compilation_unit : ( Compilation_un...
let rec tail_variable : Flambda . t -> Variable . t option = function | Var v -> Some v | Let_rec ( _ , e ) | Let_mutable { body = e } | Let { body = e ; _ } -> tail_variable e | _ -> None
let closure_symbol ( ~ backend : ( module Backend_intf . S ) ) closure_id = let module Backend = ( val backend ) in Backend . closure_symbol closure_id
let assign_symbols_and_collect_constant_definitions ( ~ backend : ( module Backend_intf . S ) ) ( ~ program : Flambda . program ) ( ~ inconstants : Inconstant_idents . result ) = let var_to_symbol_tbl = Variable . Tbl . create 42 in let var_to_definition_tbl = Variable . Tbl . cr...
let variable_field_definition ( var_to_symbol_tbl : Symbol . t Variable . Tbl . t ) ( var_to_definition_tbl : Alias_analysis . constant_defining_value Variable . Tbl . t ) ( var : Variable . t ) : Flambda . constant_defining_value_block_field = try Symbol ( Variable . Tbl . find v...