text
stringlengths
12
786k
let size t = let initial_length = initial_length t in Dwarf_int . add ( Initial_length . size initial_length ) ( Initial_length . to_dwarf_int initial_length )
let entry_to_dwarf_value ( entry : entry_and_soc_symbol ) = let adjustment = Targetint . of_int_exn entry . entry . adjustment in Dwarf_value . code_address_from_label_symbol_diff ~ comment " : ending address " ~ upper : entry . entry . addr ~ lower : entry . start_of_code_symbol ~ offset_upper : adjustment ( )
let emit ~ asm_directives t = let module A = ( val asm_directives : Asm_directives . S ) in Initial_length . emit ~ asm_directives ( initial_length t ) ; Dwarf_version . emit ~ asm_directives Dwarf_version . five ; A . uint8 ( Uint8 . of_nonnegative_int_exn Dwarf_arch_sizes . size_addr ) ; A . uint8 Uint8 . zero ; A . define_label t . base_addr ; Address_index . Map . iter ( fun _index entry -> Dwarf_value . emit ~ asm_directives ( entry_to_dwarf_value entry ) ) t . table
let afl_area_ptr dbg = Cconst_symbol ( " caml_afl_area_ptr " , dbg )
let afl_prev_loc dbg = Cconst_symbol ( " caml_afl_prev_loc " , dbg )
let afl_map_size = 1 lsl 16
let rec with_afl_logging b dbg = if ! Clflags . afl_inst_ratio < 100 && Random . int 100 >= ! Clflags . afl_inst_ratio then instrument b else let instrumentation = let cur_location = Random . int afl_map_size in let cur_pos = V . create_local " pos " in let afl_area = V . create_local " shared_mem " in let op oper args = Cop ( oper , args , dbg ) in Clet ( VP . create afl_area , op ( Cload ( Word_int , Asttypes . Mutable ) ) [ afl_area_ptr dbg ] , Clet ( VP . create cur_pos , op Cxor [ op ( Cload ( Word_int , Asttypes . Mutable ) ) [ afl_prev_loc dbg ] ; Cconst_int ( cur_location , dbg ) ] , Csequence ( op ( Cstore ( Byte_unsigned , Assignment ) ) [ op Cadda [ Cvar afl_area ; Cvar cur_pos ] ; op Cadda [ op ( Cload ( Byte_unsigned , Asttypes . Mutable ) ) [ op Cadda [ Cvar afl_area ; Cvar cur_pos ] ] ; Cconst_int ( 1 , dbg ) ] ] , op ( Cstore ( Word_int , Assignment ) ) [ afl_prev_loc dbg ; Cconst_int ( cur_location lsr 1 , dbg ) ] ) ) ) in Csequence ( instrumentation , instrument b ) | Cifthenelse ( cond , t_dbg , t , f_dbg , f , dbg ) -> Cifthenelse ( instrument cond , t_dbg , with_afl_logging t t_dbg , f_dbg , with_afl_logging f f_dbg , dbg ) | Ctrywith ( e , ex , handler , dbg ) -> Ctrywith ( instrument e , ex , with_afl_logging handler dbg , dbg ) | Cswitch ( e , cases , handlers , dbg ) -> let handlers = Array . map ( fun ( handler , handler_dbg ) -> let handler = with_afl_logging handler handler_dbg in handler , handler_dbg ) handlers in Cswitch ( instrument e , cases , handlers , dbg ) | Clet ( v , e , body ) -> Clet ( v , instrument e , instrument body ) | Clet_mut ( v , k , e , body ) -> Clet_mut ( v , k , instrument e , instrument body ) | Cphantom_let ( v , defining_expr , body ) -> Cphantom_let ( v , defining_expr , instrument body ) | Cassign ( v , e ) -> Cassign ( v , instrument e ) | Ctuple es -> Ctuple ( List . map instrument es ) | Cop ( op , es , dbg ) -> Cop ( op , List . map instrument es , dbg ) | Csequence ( e1 , e2 ) -> Csequence ( instrument e1 , instrument e2 ) | Ccatch ( isrec , cases , body ) -> let cases = List . map ( fun ( nfail , ids , e , dbg ) -> nfail , ids , instrument e , dbg ) cases in Ccatch ( isrec , cases , instrument body ) | Cexit ( ex , args ) -> Cexit ( ex , List . map instrument args ) | Cregion e -> Cregion ( instrument e ) | Ctail e -> Ctail ( instrument e ) | Cconst_int _ | Cconst_natint _ | Cconst_float _ | Cconst_symbol _ | Cvar _ as c -> c
let instrument_function c dbg = with_afl_logging c dbg
let instrument_initialiser c dbg = with_afl_logging ( Csequence ( Cop ( Cextcall ( " caml_setup_afl " , typ_int , [ ] , false ) , [ Cconst_int ( 0 , dbg ( ) ) ] , dbg ( ) ) , c ) ) ( dbg ( ) )
let total = ref 0
let err = ref 0
let res = Hashtbl . create 256
let print_title title = print_string " \ x1b [ 1 ; 37m " ; print_endline title ; print_endline ( String . make ( String . length title ) ' ' ) ; ~ print_endline " \ x1b [ 0m "
let rec update file form value = try let line = input_line file in if String . length line > 2 && not ( Hashtbl . mem res line ) then if line . [ 0 ] = ' . ' && line . [ 1 ] <> ' ' > && not ( Hashtbl . mem res line ) then Hashtbl . add res line true else if line . [ 0 ] = ' \ x1b ' && not ( Hashtbl . mem res line ) then Hashtbl . add res line false ; try let tot = Scanf . sscanf line form ( fun _ x _ -> x ) in print_endline line ; value := tot + ! value with | _ -> update file form value with End_of_file -> close_in file
let rec process n = if n = 0 then ( ) else begin let file = open_in Sys . argv . ( n ) in print_title ( Filename . chop_extension Sys . argv . ( n ) ) ; update file " Total : \ x1b [ 0 ; % dm % d % s " total ; update file " Failed : \ x1b [ 0 ; % dm % d % s " err ; begin try print_endline ( input_line file ) ; print_endline " \ n \ n " with _ -> ( ) end ; close_in file ; process ( n - 1 ) end
let print_res title total err = print_title title ; print_string " Total : \ x1b [ 0 ; 34m " ; print_int total ; print_string " \ x1b [ 0m \ nFailed : \ x1b [ 0 ; 31m " ; print_int err ; let ratio = ( . - ) 100 . @@ ( . * ) 100 . @@ ( float_of_int total |> ( . / ) @@ float_of_int err ) in print_string @@ " \ x1b [ 0m \ nRatio : \ x1b [ 0 ; 3 " ^ ( if ratio < 50 . then " 1m " else if ratio < 80 . then " 3m " else " 2m " ) ; print_float ratio ; print_endline " %\ x1b [ 0m "
let ( ) = process ( Array . length Sys . argv - 1 ) ; print_endline " . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . \ n \ n \ x1b [ 1 ; 37m " ; print_endline " " ; ~~~~~~~~~~~~~~~~~~~~~~ print_endline " | " ; | print_endline " +- Summed Results " ; -+ print_res " | " | ! total ! err ; let total , err = Hashtbl . fold ( fun _ valid ( total , err ) -> if valid then ( total + 1 , err ) else ( total + 1 , err + 1 ) ) res ( 0 , 0 ) in print_endline " \ n \ n \ x1b [ 1 ; 37m " ; print_endline " " ; ~~~~~~~~~~~~~~~~~~~~~~~ print_endline " | " ; | print_endline " [ > Unified Results ] " ; < print_res " | " | total err ; if err > 0 then exit 1
let compose_map_values_exn map ~ then_ : coercion = if Coercion . is_id coercion then map else Name . Map . map_sharing ( fun old_coercion -> Coercion . compose_exn old_coercion ~ then_ : coercion ) map
module Map_to_canonical = struct type t = coercion_to_canonical Name . Map . t let fatal_inconsistent ~ func_name elt coercion1 coercion2 = Misc . fatal_errorf " [ % s ] maps with inconsistent element / coercion couples ; % a has coercions \ % a and % a " func_name Name . print elt Coercion . print coercion1 Coercion . print coercion2 let inter map1 map2 = Name . Map . merge ( fun _elt coercion1 coercion2 -> match coercion1 , coercion2 with | None , None | Some _ , None | None , Some _ -> None | Some coercion1 , Some coercion2 -> if Coercion . equal coercion1 coercion2 then Some coercion1 else None ) map1 map2 let union map1 map2 = Name . Map . union ( fun elt coercion1 coercion2 -> match coercion1 , coercion2 with | coercion1 , coercion2 -> if Coercion . equal coercion1 coercion2 then Some coercion1 else fatal_inconsistent ~ func_name " : Aliases . Map_to_canonical . union " elt coercion1 coercion2 ) map1 map2 let all_ids_for_export t = Name . Map . fold ( fun name coercion ids -> Ids_for_export . union ids ( Ids_for_export . add_name ( Coercion . all_ids_for_export coercion ) name ) ) t Ids_for_export . empty let apply_renaming t renaming = Name . Map . fold ( fun name coercion t -> Name . Map . add ( Renaming . apply_name renaming name ) ( Coercion . apply_renaming coercion renaming ) t ) t Name . Map . empty end
module Aliases_of_canonical_element : sig type t val print : Format . formatter -> t -> unit val invariant : t -> unit val empty : t val is_empty : t -> bool val add : t -> Name . t -> coercion_to_canonical : coercion_to_canonical -> Name_mode . t -> t val find_earliest_candidates : t -> filter_by_scope ( : Name_mode . t -> Map_to_canonical . t -> Map_to_canonical . t ) -> min_name_mode : Name_mode . t -> Map_to_canonical . t option val all : t -> Map_to_canonical . t val mem : t -> Name . t -> bool val union : t -> t -> t val inter : t -> t -> t val compose : t -> then_ : Coercion . t -> t include Contains_ids . S with type t := t val apply_renaming : t -> Renaming . t -> t type t = { aliases : Map_to_canonical . t Name_mode . Map . t ; all : Map_to_canonical . t } let invariant { aliases ; all } = let aliases_union : Map_to_canonical . t = Name_mode . Map . fold ( fun _name_mode map acc -> Name . Map . union ( fun elt _coercion1 _coercion2 -> Misc . fatal_errorf " [ Aliases_of_canonical_element . invariant ] : element % a appears \ in several modes " Name . print elt ) map acc ) aliases Name . Map . empty in if Name . Map . equal Coercion . equal all aliases_union then ( ) else Misc . fatal_errorf " [ Aliases_of_canonical_element . invariant ] : [ aliases ] and [ all ] are not \ consistent " let [ @ ocamlformat " disable " ] print ppf { aliases ; all = _ ; } = Name_mode . Map . print ( Name . Map . print Coercion . print ) ppf aliases let empty = { aliases = Name_mode . Map . empty ; all = Name . Map . empty } let is_empty t = Name . Map . is_empty t . all let add t elt ~ coercion_to_canonical name_mode = if Name . Map . mem elt t . all then Misc . fatal_errorf " % a already added to [ Aliases_of_canonical_element ] : % a " Name . print elt print t ; let aliases = Name_mode . Map . update name_mode ( function | None -> Some ( Name . Map . singleton elt coercion_to_canonical ) | Some elts -> if Flambda_features . check_invariants ( ) then assert ( not ( Name . Map . mem elt elts ) ) ; Some ( Name . Map . add elt coercion_to_canonical elts ) ) t . aliases in let all = Name . Map . add elt coercion_to_canonical t . all in { aliases ; all } let find_earliest_candidates t ~ filter_by_scope ~ min_name_mode = Name_mode . Map . fold ( fun order aliases res_opt -> match res_opt with | Some _ -> res_opt | None -> begin match Name_mode . compare_partial_order order min_name_mode with | None -> None | Some result -> if result >= 0 then let aliases = filter_by_scope order aliases in if Name . Map . is_empty aliases then None else Some aliases else None end ) t . aliases None let mem t elt = Name . Map . mem elt t . all let all t = t . all let union t1 t2 = let aliases : Map_to_canonical . t Name_mode . Map . t = Name_mode . Map . union ( fun _order elts1 elts2 -> Some ( Map_to_canonical . union elts1 elts2 ) ) t1 . aliases t2 . aliases in let t = { aliases ; all = Map_to_canonical . union t1 . all t2 . all } in invariant t ; t let inter t1 t2 = let aliases = Name_mode . Map . merge ( fun _order elts1 elts2 -> match elts1 , elts2 with | None , None | Some _ , None | None , Some _ -> None | Some elts1 , Some elts2 -> Some ( Map_to_canonical . inter elts1 elts2 ) ) t1 . aliases t2 . aliases in let all = Map_to_canonical . inter t1 . all t2 . all in let t = { aliases ; all } in invariant t ; t let compose { aliases ; all } ~ then_ = let f m = Name . Map . map_sharing ( Coercion . compose_exn ~ then_ ) m in let aliases = Name_mode . Map . map_sharing f aliases in let all = f all in { aliases ; all } let all_ids_for_export { aliases ; all } = let aliases = Name_mode . Map . fold ( fun _mode map_to_canonical ids -> Ids_for_export . union ids ( Map_to_canonical . all_ids_for_export map_to_canonical ) ) aliases Ids_for_export . empty in let all = Map_to_canonical . all_ids_for_export all in Ids_for_export . union aliases all let apply_renaming { aliases ; all } renaming = let aliases = Name_mode . Map . map_sharing ( fun map_to_canonical -> Map_to_canonical . apply_renaming map_to_canonical renaming ) aliases in let all = Map_to_canonical . apply_renaming all renaming in { aliases ; all } end
module Alias_set = struct type t = { const : Reg_width_const . t option ; names : Coercion . t Name . Map . t } let empty = { const = None ; names = Name . Map . empty } let create_aliases_of_element ~ element : _ ~ canonical_element ~ coercion_from_canonical_to_element ~ alias_names_with_coercions_to_element = Simple . pattern_match canonical_element ~ const ( : fun canonical_const -> let const = Some canonical_const in let names = alias_names_with_coercions_to_element in { const ; names } ) ~ name ( : fun canonical_name ~ coercion -> assert ( Coercion . is_id coercion ) ; let const = None in let names = Name . Map . add canonical_name coercion_from_canonical_to_element alias_names_with_coercions_to_element in { const ; names } ) let singleton simple = Simple . pattern_match simple ~ const ( : fun const -> { const = Some const ; names = Name . Map . empty } ) ~ name ( : fun name ~ coercion -> { const = None ; names = Name . Map . singleton name coercion } ) let get_singleton { const ; names } = match const with | Some const -> if Name . Map . is_empty names then Some ( Simple . const const ) else None | None -> Name . Map . get_singleton names |> Option . map ( fun ( name , coercion ) -> Simple . with_coercion ( Simple . name name ) coercion ) let [ @ ocamlformat " disable " ] print ppf { const ; names ; } = let none ppf ( ) = Format . fprintf ppf " @< 0 >% s ( ) " ( Flambda_colours . elide ( ) ) in Format . fprintf ppf " [ @< hov 1 ( >\ [ @< hov 1 ( > const @ % a ) ] @@ \ [ @< hov 1 ( > names @ % a ) ] @\ ] " @ ( Format . pp_print_option Reg_width_const . print ~ none ) const ( Name . Map . print Coercion . print ) names let inter { const = const1 ; names = names1 } { const = const2 ; names = names2 } = let const = match const1 , const2 with | Some const1 , Some const2 when Reg_width_const . equal const1 const2 -> Some const1 | _ , _ -> None in let names = Map_to_canonical . inter names1 names2 in { const ; names } let filter { const ; names } ~ f = let const = match const with | Some const when f ( Simple . const const ) -> Some const | _ -> None in let names = Name . Map . filter ( fun name coercion -> let simple = Simple . with_coercion ( Simple . name name ) coercion in f simple ) names in { const ; names } let find_best { const ; names } = match const with | Some const -> Some ( Simple . const const ) | None -> ( let key_is_symbol key _data = Name . is_symbol key in let symbols , vars = Name . Map . partition key_is_symbol names in match Name . Map . min_binding_opt symbols with | Some ( symbol , coercion ) -> Some ( Simple . with_coercion ( Simple . name symbol ) coercion ) | None -> ( match Name . Map . min_binding_opt vars with | Some ( var , coercion ) -> Some ( Simple . with_coercion ( Simple . name var ) coercion ) | None -> None ) ) end
type t = { canonical_elements : ( Simple . t * coercion_to_canonical ) Name . Map . t ; aliases_of_canonical_names : Aliases_of_canonical_element . t Name . Map . t ; aliases_of_consts : Aliases_of_canonical_element . t Reg_width_const . Map . t }
let [ @ ocamlformat " disable " ] print ppf { canonical_elements ; aliases_of_canonical_names ; aliases_of_consts ; } = let print_element_and_coercion ppf ( elt , coercion ) = Format . fprintf ppf " [ @< hov 1 ( >\ % a @ \ [ @< hov 1 >@< 0 >% s ( coercion @ % a ) @< 0 >% s ] @\ ) ] " @ Simple . print elt ( if Coercion . is_id coercion then Flambda_colours . elide ( ) else Flambda_colours . normal ( ) ) Coercion . print coercion ( Flambda_colours . normal ( ) ) in Format . fprintf ppf " [ @< hov 1 ( >\ [ @< hov 1 ( > canonical_elements @ % a ) ] @@ \ [ @< hov 1 ( > aliases_of_canonical_names @ % a ) ] @@ \ [ @< hov 1 ( > aliases_of_consts @ % a ) ] @\ ) ] " @ ( Name . Map . print print_element_and_coercion ) canonical_elements ( Name . Map . print Aliases_of_canonical_element . print ) aliases_of_canonical_names ( Reg_width_const . Map . print Aliases_of_canonical_element . print ) aliases_of_consts
let name_defined_earlier ~ binding_time_resolver ~ binding_times_and_modes alias ~ than = if Name . equal alias than || ( Name . is_symbol alias && Name . is_symbol than ) then false else if Name . is_symbol alias && not ( Name . is_symbol than ) then true else if Name . is_symbol than && not ( Name . is_symbol alias ) then false else let alias_binding_time = match Name . Map . find alias binding_times_and_modes with | exception Not_found -> Binding_time . imported_variables | _ , binding_time_and_mode -> Binding_time . With_name_mode . binding_time binding_time_and_mode in let than_binding_time = match Name . Map . find than binding_times_and_modes with | exception Not_found -> Binding_time . imported_variables | _ , binding_time_and_mode -> Binding_time . With_name_mode . binding_time binding_time_and_mode in if Binding_time . strictly_earlier alias_binding_time ~ than : than_binding_time then true else if not ( Binding_time . equal alias_binding_time than_binding_time ) then false else let alias_comp_unit = Name . compilation_unit alias in let than_comp_unit = Name . compilation_unit than in let c = Compilation_unit . compare alias_comp_unit than_comp_unit in if c < 0 then true else if c > 0 then false else let alias_binding_time = binding_time_resolver alias |> Binding_time . With_name_mode . binding_time in let than_binding_time = binding_time_resolver than |> Binding_time . With_name_mode . binding_time in Binding_time . strictly_earlier alias_binding_time ~ than : than_binding_time
let defined_earlier ~ binding_time_resolver ~ binding_times_and_modes alias ~ than = Simple . pattern_match than ~ const ( : fun _ -> false ) ~ name ( : fun than ~ coercion : _ -> Simple . pattern_match alias ~ const ( : fun _ -> true ) ~ name ( : fun alias ~ coercion : _ -> name_defined_earlier ~ binding_time_resolver ~ binding_times_and_modes alias ~ than ) )
let binding_time_and_name_mode ~ binding_times_and_modes elt = Simple . pattern_match ' elt ~ const ( : fun _ -> Binding_time . With_name_mode . consts ) ~ var ( : fun var ~ coercion : _ -> let name = Name . var var in match Name . Map . find name binding_times_and_modes with | _ , binding_time_and_mode -> binding_time_and_mode | exception Not_found -> Binding_time . With_name_mode . imported_variables ) ~ symbol ( : fun _ ~ coercion : _ -> Binding_time . With_name_mode . symbols )
let compute_name_mode_unscoped ~ binding_times_and_modes elt = Binding_time . With_name_mode . name_mode ( binding_time_and_name_mode ~ binding_times_and_modes elt )
let compute_name_mode ~ binding_times_and_modes elt ~ min_binding_time = Binding_time . With_name_mode . scoped_name_mode ( binding_time_and_name_mode ~ binding_times_and_modes elt ) ~ min_binding_time
let invariant ~ binding_time_resolver ~ binding_times_and_modes t = if Flambda_features . check_invariants ( ) then let all_aliases_of_names : Map_to_canonical . t = Name . Map . fold ( fun canonical_element aliases all_aliases -> Aliases_of_canonical_element . invariant aliases ; let aliases = Aliases_of_canonical_element . all aliases in if not ( Name . Map . for_all ( fun elt _coercion -> name_defined_earlier ~ binding_time_resolver ~ binding_times_and_modes canonical_element ~ than : elt ) aliases ) then Misc . fatal_errorf " Canonical element % a is not earlier than all of its aliases :@ % a " Name . print canonical_element print t ; if Name . Map . mem canonical_element aliases then Misc . fatal_errorf " Canonical element % a occurs in alias set :@ % a " Name . print canonical_element ( Name . Map . print Coercion . print ) aliases ; if Name . Map . inter_domain_is_non_empty aliases all_aliases then Misc . fatal_errorf " Overlapping alias sets :@ % a " print t ; Map_to_canonical . union aliases all_aliases ) t . aliases_of_canonical_names Name . Map . empty in let _all_aliases : Map_to_canonical . t = Reg_width_const . Map . fold ( fun _const aliases all_aliases -> Aliases_of_canonical_element . invariant aliases ; let aliases = Aliases_of_canonical_element . all aliases in if Name . Map . inter_domain_is_non_empty aliases all_aliases then Misc . fatal_errorf " Overlapping alias sets :@ % a " print t ; Name . Map . disjoint_union aliases all_aliases ) t . aliases_of_consts all_aliases_of_names in ( )
let empty = { canonical_elements = Name . Map . empty ; aliases_of_canonical_names = Name . Map . empty ; aliases_of_consts = Reg_width_const . Map . empty }
type canonical = | Is_canonical | Alias_of_canonical of { canonical_element : Simple . t ; coercion_to_canonical : coercion_to_canonical }
let canonical t element : canonical = Simple . pattern_match element ~ const ( : fun _ -> Is_canonical ) ~ name ( : fun name ~ coercion : coercion_from_bare_element_to_element -> match Name . Map . find name t . canonical_elements with | exception Not_found -> Is_canonical | canonical_element , coercion_from_bare_element_to_canonical -> let coercion_from_element_to_bare_element = Coercion . inverse coercion_from_bare_element_to_element in let coercion_from_element_to_canonical = Coercion . compose_exn coercion_from_element_to_bare_element ~ then_ : coercion_from_bare_element_to_canonical in if Flambda_features . check_invariants ( ) then assert ( not ( Simple . equal element canonical_element ) ) ; Alias_of_canonical { canonical_element ; coercion_to_canonical = coercion_from_element_to_canonical } )
let get_aliases_of_canonical_element t ~ canonical_element = assert ( not ( Simple . has_coercion canonical_element ) ) ; let name name ~ coercion : _ = Name . Map . find name t . aliases_of_canonical_names in let const const = Reg_width_const . Map . find const t . aliases_of_consts in match Simple . pattern_match canonical_element ~ name ~ const with | exception Not_found -> Aliases_of_canonical_element . empty | aliases -> aliases
let add_alias_between_canonical_elements ~ binding_time_resolver ~ binding_times_and_modes t ~ canonical_element ~ coercion_to_canonical ~ to_be_demoted = if Simple . equal canonical_element to_be_demoted then if Coercion . is_id coercion_to_canonical then t else Misc . fatal_errorf " Cannot add an alias of % a @ to itself with a non - identity coercion @ % a " Simple . print canonical_element Coercion . print coercion_to_canonical else let name_to_be_demoted = assert ( not ( Simple . has_coercion to_be_demoted ) ) ; Simple . pattern_match to_be_demoted ~ const ( : fun c -> Misc . fatal_errorf " Can ' t demote const % a @ ( while adding alias to @ % a ) " Reg_width_const . print c Simple . print canonical_element ) ~ name ( : fun name ~ coercion : _ -> name ) in let aliases_of_to_be_demoted = get_aliases_of_canonical_element t ~ canonical_element : to_be_demoted in if Flambda_features . check_invariants ( ) then Simple . pattern_match canonical_element ~ const ( : fun _ -> ( ) ) ~ name ( : fun canonical_element ~ coercion -> assert ( Coercion . is_id coercion ) ; assert ( not ( Aliases_of_canonical_element . mem aliases_of_to_be_demoted canonical_element ) ) ) ; let canonical_elements = t . canonical_elements |> Name . Map . fold ( fun alias coercion_to_to_be_demoted canonical_elements -> let coercion_to_canonical = Coercion . compose_exn coercion_to_to_be_demoted ~ then_ : coercion_to_canonical in Name . Map . add alias ( canonical_element , coercion_to_canonical ) canonical_elements ) ( Aliases_of_canonical_element . all aliases_of_to_be_demoted ) |> Name . Map . add name_to_be_demoted ( canonical_element , coercion_to_canonical ) in let aliases_of_canonical_element = get_aliases_of_canonical_element t ~ canonical_element in if Flambda_features . check_invariants ( ) then begin assert ( not ( Aliases_of_canonical_element . mem aliases_of_canonical_element name_to_be_demoted ) ) ; assert ( Aliases_of_canonical_element . is_empty ( Aliases_of_canonical_element . inter aliases_of_canonical_element aliases_of_to_be_demoted ) ) end ; let aliases = Aliases_of_canonical_element . add ( Aliases_of_canonical_element . union ( Aliases_of_canonical_element . compose aliases_of_to_be_demoted ~ then_ : coercion_to_canonical ) aliases_of_canonical_element ) name_to_be_demoted ~ coercion_to_canonical ( compute_name_mode_unscoped ~ binding_times_and_modes to_be_demoted ) in let aliases_of_canonical_names = Name . Map . remove name_to_be_demoted t . aliases_of_canonical_names in let aliases_of_canonical_names , aliases_of_consts = assert ( not ( Simple . has_coercion canonical_element ) ) ; Simple . pattern_match canonical_element ~ name ( : fun name ~ coercion : _ -> ( Name . Map . add name aliases aliases_of_canonical_names , t . aliases_of_consts ) ) ~ const ( : fun const -> ( aliases_of_canonical_names , Reg_width_const . Map . add const aliases t . aliases_of_consts ) ) in let res = { canonical_elements ; aliases_of_canonical_names ; aliases_of_consts } in invariant ~ binding_time_resolver ~ binding_times_and_modes res ; res
type to_be_demoted = | Demote_canonical_element1 | Demote_canonical_element2
let choose_canonical_element_to_be_demoted ~ binding_time_resolver ~ binding_times_and_modes ~ canonical_element1 ~ canonical_element2 = if defined_earlier ~ binding_time_resolver ~ binding_times_and_modes canonical_element1 ~ than : canonical_element2 then Demote_canonical_element2 else Demote_canonical_element1
type add_result = { t : t ; canonical_element : Simple . t ; alias_of_demoted_element : Simple . t }
let invariant_add_result ~ binding_time_resolver ~ binding_times_and_modes ~ original_t { canonical_element ; alias_of_demoted_element ; t } = if Flambda_features . check_invariants ( ) then begin invariant ~ binding_time_resolver ~ binding_times_and_modes t ; if not ( defined_earlier ~ binding_time_resolver ~ binding_times_and_modes canonical_element ~ than : alias_of_demoted_element ) then Misc . fatal_errorf " Canonical element % a should be defined earlier than % a after alias \ addition . @ Original alias tracker :@ % a @ Resulting alias tracker :@ % a " Simple . print canonical_element Simple . print alias_of_demoted_element print original_t print t ; match canonical t alias_of_demoted_element with | Is_canonical -> Misc . fatal_errorf " Alias % a must not be must not be canonical anymore . @ Original alias \ tracker :@ % a @ Resulting alias tracker :@ % a " Simple . print alias_of_demoted_element print original_t print t | Alias_of_canonical _ -> ( ) end
let add_alias ~ binding_time_resolver ~ binding_times_and_modes t ~ canonical_element1 ~ coercion_from_canonical_element2_to_canonical_element1 ~ canonical_element2 = assert ( not ( Simple . has_coercion canonical_element1 ) ) ; assert ( not ( Simple . has_coercion canonical_element2 ) ) ; let ( canonical_element , demoted_canonical , coercion_from_demoted_canonical_to_canonical ) = let which_element = choose_canonical_element_to_be_demoted ~ binding_time_resolver ~ binding_times_and_modes ~ canonical_element1 ~ canonical_element2 in match which_element with | Demote_canonical_element1 -> let coercion_from_canonical_element1_to_canonical_element2 = Coercion . inverse coercion_from_canonical_element2_to_canonical_element1 in ( canonical_element2 , canonical_element1 , coercion_from_canonical_element1_to_canonical_element2 ) | Demote_canonical_element2 -> ( canonical_element1 , canonical_element2 , coercion_from_canonical_element2_to_canonical_element1 ) in let t = add_alias_between_canonical_elements ~ binding_time_resolver ~ binding_times_and_modes t ~ canonical_element ~ coercion_to_canonical : coercion_from_demoted_canonical_to_canonical ~ to_be_demoted : demoted_canonical in let alias_of_demoted_element = Simple . with_coercion demoted_canonical coercion_from_demoted_canonical_to_canonical in { t ; canonical_element ; alias_of_demoted_element }
let add ~ binding_time_resolver ~ binding_times_and_modes t ~ canonical_element1 : element1_with_coercion ~ canonical_element2 : element2_with_coercion = let original_t = t in let canonical_element1 = element1_with_coercion |> Simple . without_coercion in let canonical_element2 = element2_with_coercion |> Simple . without_coercion in let coercion_from_canonical_element2_to_canonical_element1 = Coercion . compose_exn ( Simple . coercion element2_with_coercion ) ~ then_ ( : Coercion . inverse ( Simple . coercion element1_with_coercion ) ) in if Flambda_features . check_invariants ( ) then begin if Simple . equal canonical_element1 canonical_element2 then Misc . fatal_errorf " Cannot alias an element to itself : % a " Simple . print canonical_element1 ; Simple . pattern_match canonical_element1 ~ name ( : fun _ ~ coercion : _ -> ( ) ) ~ const ( : fun const1 -> Simple . pattern_match canonical_element2 ~ name ( : fun _ ~ coercion : _ -> ( ) ) ~ const ( : fun const2 -> Misc . fatal_errorf " Cannot add alias between two consts : % a , % a " Reg_width_const . print const1 Reg_width_const . print const2 ) ) end ; let add_result = add_alias ~ binding_time_resolver ~ binding_times_and_modes t ~ canonical_element1 ~ coercion_from_canonical_element2_to_canonical_element1 ~ canonical_element2 in if Flambda_features . check_invariants ( ) then invariant_add_result ~ binding_time_resolver ~ binding_times_and_modes ~ original_t add_result ; add_result
let find_earliest_alias t ~ canonical_element ~ binding_times_and_modes ~ min_binding_time ~ min_name_mode ~ binding_time_resolver ~ coercion_from_canonical_to_element = let aliases = get_aliases_of_canonical_element t ~ canonical_element in let filter_by_scope name_mode names = if Name_mode . equal name_mode Name_mode . in_types then names else Name . Map . filter ( fun name _coercion -> let scoped_name_mode = compute_name_mode ~ binding_times_and_modes ( Simple . name name ) ~ min_binding_time in Name_mode . equal name_mode scoped_name_mode ) names in match Aliases_of_canonical_element . find_earliest_candidates aliases ~ filter_by_scope ~ min_name_mode with | Some at_earliest_mode -> assert ( not ( Name . Map . is_empty at_earliest_mode ) ) ; let earliest , coercion_from_earliest_to_canonical = Name . Map . fold ( fun elt coercion ( ( min_elt , _min_coercion ) as min_binding ) -> if name_defined_earlier ~ binding_time_resolver ~ binding_times_and_modes elt ~ than : min_elt then elt , coercion else min_binding ) at_earliest_mode ( Name . Map . min_binding at_earliest_mode ) in let coercion_from_earliest_to_element = Coercion . compose_exn coercion_from_earliest_to_canonical ~ then_ : coercion_from_canonical_to_element in Simple . with_coercion ( Simple . name earliest ) coercion_from_earliest_to_element | None -> raise Not_found
let get_canonical_element_exn ~ binding_time_resolver ~ binding_times_and_modes t element elt_name_mode ~ min_name_mode ~ min_binding_time = let canonical = canonical t element in match canonical with | Is_canonical when match Name_mode . compare_partial_order elt_name_mode min_name_mode with | None -> false | Some c -> c >= 0 -> element | Is_canonical | Alias_of_canonical _ -> ( let canonical_element , name_mode , coercion_from_canonical_to_element = match canonical with | Is_canonical -> Simple . without_coercion element , elt_name_mode , Simple . coercion element | Alias_of_canonical { canonical_element ; coercion_to_canonical } -> let name_mode = compute_name_mode ~ binding_times_and_modes canonical_element ~ min_binding_time in canonical_element , name_mode , Coercion . inverse coercion_to_canonical in assert ( not ( Simple . has_coercion canonical_element ) ) ; match Name_mode . compare_partial_order name_mode min_name_mode with | None -> find_earliest_alias t ~ canonical_element ~ binding_times_and_modes ~ min_binding_time ~ min_name_mode ~ binding_time_resolver ~ coercion_from_canonical_to_element | Some c -> if c >= 0 then Simple . with_coercion canonical_element coercion_from_canonical_to_element else find_earliest_alias t ~ canonical_element ~ binding_times_and_modes ~ min_binding_time ~ min_name_mode ~ binding_time_resolver ~ coercion_from_canonical_to_element )
let get_aliases t element = match canonical t element with | Is_canonical -> let canonical_element = Simple . without_coercion element in assert ( not ( Simple . has_coercion canonical_element ) ) ; let alias_names_with_coercions_to_canonical = Aliases_of_canonical_element . all ( get_aliases_of_canonical_element t ~ canonical_element ) in let coercion_from_canonical_to_element = Simple . coercion element in let alias_names_with_coercions_to_element = compose_map_values_exn alias_names_with_coercions_to_canonical ~ then_ : coercion_from_canonical_to_element in Alias_set . create_aliases_of_element ~ element ~ canonical_element ~ coercion_from_canonical_to_element ~ alias_names_with_coercions_to_element | Alias_of_canonical { canonical_element ; coercion_to_canonical = coercion_from_element_to_canonical } -> assert ( not ( Simple . has_coercion canonical_element ) ) ; assert ( not ( Simple . equal ( Simple . without_coercion element ) canonical_element ) ) ; let alias_names_with_coercions_to_canonical = Aliases_of_canonical_element . all ( get_aliases_of_canonical_element t ~ canonical_element ) in let coercion_from_canonical_to_element = Coercion . inverse coercion_from_element_to_canonical in let alias_names_with_coercions_to_element = compose_map_values_exn alias_names_with_coercions_to_canonical ~ then_ : coercion_from_canonical_to_element in ( if Flambda_features . check_invariants ( ) then let element_coerced_to_canonical = Simple . apply_coercion_exn element coercion_from_element_to_canonical in assert ( Name . Map . exists ( fun name coercion_from_name_to_canonical -> let name_coerced_to_canonical = Simple . apply_coercion_exn ( Simple . name name ) coercion_from_name_to_canonical in Simple . equal element_coerced_to_canonical name_coerced_to_canonical ) alias_names_with_coercions_to_canonical ) ) ; Alias_set . create_aliases_of_element ~ element ~ canonical_element ~ coercion_from_canonical_to_element ~ alias_names_with_coercions_to_element
let all_ids_for_export { canonical_elements ; aliases_of_canonical_names ; aliases_of_consts } = let ids = Name . Map . fold ( fun elt ( canonical , coercion ) ids -> let ids = Ids_for_export . union ids ( Coercion . all_ids_for_export coercion ) in Ids_for_export . add_name ( Ids_for_export . add_simple ids canonical ) elt ) canonical_elements Ids_for_export . empty in let ids = Name . Map . fold ( fun elt aliases_of ids -> Ids_for_export . add_name ( Ids_for_export . union ids ( Aliases_of_canonical_element . all_ids_for_export aliases_of ) ) elt ) aliases_of_canonical_names ids in Reg_width_const . Map . fold ( fun const aliases_of ids -> Ids_for_export . add_const ( Ids_for_export . union ids ( Aliases_of_canonical_element . all_ids_for_export aliases_of ) ) const ) aliases_of_consts ids
let apply_renaming { canonical_elements ; aliases_of_canonical_names ; aliases_of_consts } renaming = let rename_name = Renaming . apply_name renaming in let rename_simple = Renaming . apply_simple renaming in let canonical_elements = Name . Map . fold ( fun elt ( canonical , coercion ) acc -> Name . Map . add ( rename_name elt ) ( rename_simple canonical , Coercion . apply_renaming coercion renaming ) acc ) canonical_elements Name . Map . empty in let aliases_of_canonical_names = Name . Map . fold ( fun canonical aliases acc -> Name . Map . add ( rename_name canonical ) ( Aliases_of_canonical_element . apply_renaming aliases renaming ) acc ) aliases_of_canonical_names Name . Map . empty in let aliases_of_consts = Reg_width_const . Map . fold ( fun const aliases -> Reg_width_const . Map . add ( Renaming . apply_const renaming const ) ( Aliases_of_canonical_element . apply_renaming aliases renaming ) ) aliases_of_consts Reg_width_const . Map . empty in { canonical_elements ; aliases_of_canonical_names ; aliases_of_consts }
let merge t1 t2 = let canonical_elements = Name . Map . disjoint_union t1 . canonical_elements t2 . canonical_elements in ( if Flambda_features . check_invariants ( ) then let empty_maps = Name . Map . is_empty t1 . aliases_of_canonical_names && Reg_width_const . Map . is_empty t1 . aliases_of_consts && Name . Map . is_empty t2 . aliases_of_canonical_names && Reg_width_const . Map . is_empty t2 . aliases_of_consts in if empty_maps then ( ) else Misc . fatal_errorf " Aliases . merge : at least one of the inputs has an unexpected non - empty \ field . . @ t1 : % a . @ t2 : % a . " @ print t1 print t2 ) ; { canonical_elements ; aliases_of_canonical_names = Name . Map . empty ; aliases_of_consts = Reg_width_const . Map . empty }
let get_canonical_ignoring_name_mode t name = let elt = Simple . name name in match canonical t elt with | Is_canonical -> elt | Alias_of_canonical { canonical_element ; coercion_to_canonical } -> let coercion_from_canonical = Coercion . inverse coercion_to_canonical in Simple . apply_coercion_exn canonical_element coercion_from_canonical
let clean_for_export { canonical_elements ; aliases_of_canonical_names = _ ; aliases_of_consts = _ } ~ reachable_names = let canonical_elements = Name . Map . filter ( fun name _canonical_and_coercion_to_canonical -> ( not ( Name . is_imported name ) ) && Name_occurrences . mem_name reachable_names name ) canonical_elements in { canonical_elements ; aliases_of_canonical_names = Name . Map . empty ; aliases_of_consts = Reg_width_const . Map . empty }
type allocation_point = | Symbol of Symbol . t | Variable of Variable . t
type allocated_const = | Normal of Allocated_const . t | Array of Lambda . array_kind * Lambda . mutable_flag * Variable . t list | Duplicate_array of Lambda . array_kind * Lambda . mutable_flag * Variable . t
type constant_defining_value = | Allocated_const of allocated_const | Block of Tag . t * Variable . t list | Set_of_closures of Flambda . set_of_closures | Project_closure of Flambda . project_closure | Move_within_set_of_closures of Flambda . move_within_set_of_closures | Project_var of Flambda . project_var | Field of Variable . t * int | Symbol_field of Symbol . t * int | Const of Flambda . const | Symbol of Symbol . t | Variable of Variable . t
type initialize_symbol_field = Variable . t option
type definitions = { variable : constant_defining_value Variable . Tbl . t ; initialize_symbol : initialize_symbol_field list Symbol . Tbl . t ; symbol : Flambda . constant_defining_value Symbol . Tbl . t ; }
let print_constant_defining_value ppf = function | Allocated_const ( Normal const ) -> Allocated_const . print ppf const | Allocated_const ( Array ( _ , _ , vars ) ) -> Format . fprintf ppf " [ | % a ] " | ( Format . pp_print_list Variable . print ) vars | Allocated_const ( Duplicate_array ( _ , _ , var ) ) -> Format . fprintf ppf " dup_array ( % a ) " Variable . print var | Block ( tag , vars ) -> Format . fprintf ppf " [ |% a : % a ] " | Tag . print tag ( Format . pp_print_list Variable . print ) vars | Set_of_closures set -> Flambda . print_set_of_closures ppf set | Project_closure project -> Flambda . print_project_closure ppf project | Move_within_set_of_closures move -> Flambda . print_move_within_set_of_closures ppf move | Project_var project -> Flambda . print_project_var ppf project | Field ( var , field ) -> Format . fprintf ppf " % a . ( % d ) " Variable . print var field | Symbol_field ( sym , field ) -> Format . fprintf ppf " % a . ( % d ) " Symbol . print sym field | Const const -> Flambda . print_const ppf const | Symbol symbol -> Symbol . print ppf symbol | Variable var -> Variable . print ppf var
let rec resolve_definition ( definitions : definitions ) ( var : Variable . t ) ( def : constant_defining_value ) ~ the_dead_constant : allocation_point = match def with | Allocated_const _ | Block _ | Set_of_closures _ | Project_closure _ | Const _ | Move_within_set_of_closures _ -> Variable var | Project_var { var } -> fetch_variable definitions ( Var_within_closure . unwrap var ) ~ the_dead_constant | Variable v -> fetch_variable definitions v ~ the_dead_constant | Symbol sym -> Symbol sym | Field ( v , n ) -> begin match fetch_variable definitions v ~ the_dead_constant with | Symbol s -> fetch_symbol_field definitions s n ~ the_dead_constant | Variable v -> fetch_variable_field definitions v n ~ the_dead_constant end | Symbol_field ( symbol , field ) -> fetch_symbol_field definitions symbol field ~ the_dead_constant ( definitions : definitions ) ( var : Variable . t ) ~ the_dead_constant : allocation_point = match Variable . Tbl . find definitions . variable var with | exception Not_found -> Variable var | def -> resolve_definition definitions var def ~ the_dead_constant ( definitions : definitions ) ( var : Variable . t ) ( field : int ) ~ the_dead_constant : allocation_point = match Variable . Tbl . find definitions . variable var with | Block ( _ , fields ) -> begin match List . nth fields field with | exception Not_found -> Symbol the_dead_constant | v -> fetch_variable definitions v ~ the_dead_constant end | exception Not_found -> Misc . fatal_errorf " No definition for field access to % a " Variable . print var | Symbol _ | Variable _ | Project_var _ | Field _ | Symbol_field _ -> assert false | Const _ | Allocated_const _ | Set_of_closures _ | Project_closure _ | Move_within_set_of_closures _ -> Symbol the_dead_constant ( definitions : definitions ) ( sym : Symbol . t ) ( field : int ) ~ the_dead_constant : allocation_point = match Symbol . Tbl . find definitions . symbol sym with | Block ( _ , fields ) -> begin match List . nth fields field with | exception Not_found -> Symbol the_dead_constant | Symbol s -> Symbol s | Const _ -> Symbol sym end | exception Not_found -> begin match Symbol . Tbl . find definitions . initialize_symbol sym with | fields -> begin match List . nth fields field with | None -> Misc . fatal_errorf " Constant field access to an inconstant % a " Symbol . print sym | Some v -> fetch_variable definitions v ~ the_dead_constant end | exception Not_found -> Misc . fatal_errorf " No definition for field access to % a " Symbol . print sym end | Allocated_const _ | Set_of_closures _ | Project_closure _ -> Symbol the_dead_constant
let run variable initialize_symbol symbol ~ the_dead_constant = let definitions = { variable ; initialize_symbol ; symbol ; } in Variable . Tbl . fold ( fun var definition result -> let definition = resolve_definition definitions var definition ~ the_dead_constant in Variable . Map . add var definition result ) definitions . variable Variable . Map . empty
let ar = Array . create l " " ; ;
let compact_flag = ref false ; ;
let main ( ) = while true do for i = 1 to 100000 do ar . ( Random . int l ) <- String . create ( Random . int m ) ; done ; if ! compact_flag then Gc . compact ( ) else Gc . full_major ( ) ; print_newline ( ) ; Gc . print_stat stdout ; flush stdout ; done ; ;
let argspecs = [ " - c " , Arg . Set compact_flag , " do heap compactions " ; ] ; ;
type t = | Float of float | Int32 of int32 | Int64 of int64 | Nativeint of nativeint | Float_array of float list | Immutable_float_array of float list | String of string | Immutable_string of string
let compare_floats x1 x2 = Int64 . compare ( Int64 . bits_of_float x1 ) ( Int64 . bits_of_float x2 )
let compare ( x : t ) ( y : t ) = let rec compare_float_lists l1 l2 = match l1 , l2 with | [ ] , [ ] -> 0 | [ ] , _ :: _ -> - 1 | _ :: _ , [ ] -> 1 | h1 :: t1 , h2 :: t2 -> let c = compare_floats h1 h2 in if c <> 0 then c else compare_float_lists t1 t2 in match x , y with | Float x , Float y -> compare_floats x y | Int32 x , Int32 y -> Int32 . compare x y | Int64 x , Int64 y -> Int64 . compare x y | Nativeint x , Nativeint y -> Nativeint . compare x y | Float_array x , Float_array y -> compare_float_lists x y | Immutable_float_array x , Immutable_float_array y -> compare_float_lists x y | String x , String y -> String . compare x y | Immutable_string x , Immutable_string y -> String . compare x y | Float _ , _ -> - 1 | _ , Float _ -> 1 | Int32 _ , _ -> - 1 | _ , Int32 _ -> 1 | Int64 _ , _ -> - 1 | _ , Int64 _ -> 1 | Nativeint _ , _ -> - 1 | _ , Nativeint _ -> 1 | Float_array _ , _ -> - 1 | _ , Float_array _ -> 1 | Immutable_float_array _ , _ -> - 1 | _ , Immutable_float_array _ -> 1 | String _ , _ -> - 1 | _ , String _ -> 1
let print ppf ( t : t ) = let fprintf = Format . fprintf in let floats ppf fl = List . iter ( fun f -> fprintf ppf " @ % f " f ) fl in match t with | String s -> fprintf ppf " % S " s | Immutable_string s -> fprintf ppf " #% S " s | Int32 n -> fprintf ppf " % lil " n | Int64 n -> fprintf ppf " % LiL " n | Nativeint n -> fprintf ppf " % nin " n | Float f -> fprintf ppf " % f " f | Float_array [ ] -> fprintf ppf " [ | ] " | | Float_array ( f1 :: fl ) -> fprintf ppf " [ @< 1 [ [ >|@% f % a ] ] ] " @|@ f1 floats fl | Immutable_float_array [ ] -> fprintf ppf " [ |# ] " | | Immutable_float_array ( f1 :: fl ) -> fprintf ppf " [ @< 1 [ >|# [ @% f % a ] ] ] " @|@ f1 floats fl
let twopi = 2 . 0 . * pic
let test_loops = 5 [ | 0 . 3870983098 ; 0 . 0 ; 0 . 0 ] ; | [ | 0 . 7233298200 ; 0 . 0 ; 0 . 0 ] ; | [ | 1 . 0000010178 ; 0 . 0 ; 0 . 0 ] ; | [ | 1 . 5236793419 ; 3e - 10 ; 0 . 0 ] ; | [ | 5 . 2026032092 ; 19132e - 10 ; - 39e - 10 ] ; | [ | 9 . 5549091915 ; - 0 . 0000213896 ; 444e - 10 ] ; | [ | 19 . 2184460618 ; - 3716e - 10 ; 979e - 10 ] ; | [ | 30 . 1103868694 ; - 16635e - 10 ; 686e - 10 ] | ] | [ | [ | 252 . 25090552 ; 5381016286 . 88982 ; - 1 . 92789 ] ; | [ | 181 . 97980085 ; 2106641364 . 33548 ; 0 . 59381 ] ; | [ | 100 . 46645683 ; 1295977422 . 83429 ; - 2 . 04411 ] ; | [ | 355 . 43299958 ; 689050774 . 93988 ; 0 . 94264 ] ; | [ | 34 . 35151874 ; 109256603 . 77991 ; - 30 . 60378 ] ; | [ | 50 . 07744430 ; 43996098 . 55732 ; 75 . 61614 ] ; | [ | 314 . 05500511 ; 15424811 . 93933 ; - 1 . 75083 ] ; | [ | 304 . 34866548 ; 7865503 . 20744 ; 0 . 21103 ] | ] | [ | [ | 0 . 2056317526 ; 0 . 0002040653 ; - 28349e - 10 ] ; | [ | 0 . 0067719164 ; - 0 . 0004776521 ; 98127e - 10 ] ; | [ | 0 . 0167086342 ; - 0 . 0004203654 ; - 0 . 0000126734 ] ; | [ | 0 . 0934006477 ; 0 . 0009048438 ; - 80641e - 10 ] ; | [ | 0 . 0484979255 ; 0 . 0016322542 ; - 0 . 0000471366 ] ; | [ | 0 . 0555481426 ; - 0 . 0034664062 ; - 0 . 0000643639 ] ; | [ | 0 . 0463812221 ; - 0 . 0002729293 ; 0 . 0000078913 ] ; | [ | 0 . 0094557470 ; 0 . 0000603263 ; 0 . 0 ] | ] | [ | [ | 77 . 45611904 ; 5719 . 11590 ; - 4 . 83016 ] ; | [ | 131 . 56370300 ; 175 . 48640 ; - 498 . 48184 ] ; | [ | 102 . 93734808 ; 11612 . 35290 ; 53 . 27577 ] ; | [ | 336 . 06023395 ; 15980 . 45908 ; - 62 . 32800 ] ; | [ | 14 . 33120687 ; 7758 . 75163 ; 259 . 95938 ] ; | [ | 93 . 05723748 ; 20395 . 49439 ; 190 . 25952 ] ; | [ | 173 . 00529106 ; 3215 . 56238 ; - 34 . 09288 ] ; | [ | 48 . 12027554 ; 1050 . 71912 ; 27 . 39717 ] | ] | [ | [ | 7 . 00498625 ; - 214 . 25629 ; 0 . 28977 ] ; | [ | 3 . 39466189 ; - 30 . 84437 ; - 11 . 67836 ] ; | [ | 0 . 0 ; 469 . 97289 ; - 3 . 35053 ] ; | [ | 1 . 84972648 ; - 293 . 31722 ; - 8 . 11830 ] ; | [ | 1 . 30326698 ; - 71 . 55890 ; 11 . 95297 ] ; | [ | 2 . 48887878 ; 91 . 85195 ; - 17 . 66225 ] ; | [ | 0 . 77319689 ; - 60 . 72723 ; 1 . 25759 ] ; | [ | 1 . 76995259 ; 8 . 12333 ; 0 . 08135 ] | ] | [ | [ | 48 . 33089304 ; - 4515 . 21727 ; - 31 . 79892 ] ; | [ | 76 . 67992019 ; - 10008 . 48154 ; - 51 . 32614 ] ; | [ | 174 . 87317577 ; - 8679 . 27034 ; 15 . 34191 ] ; | [ | 49 . 55809321 ; - 10620 . 90088 ; - 230 . 57416 ] ; | [ | 100 . 46440702 ; 6362 . 03561 ; 326 . 52178 ] ; | [ | 113 . 66550252 ; - 9240 . 19942 ; - 66 . 23743 ] ; | [ | 74 . 00595701 ; 2669 . 15033 ; 145 . 93964 ] ; | [ | 131 . 78405702 ; - 221 . 94322 ; - 0 . 78728 ] | ] | [ | [ | 69613 . 0 ; 75645 . 0 ; 88306 . 0 ; 59899 . 0 ; 15746 . 0 ; 71087 . 0 ; 142173 . 0 ; 3086 . 0 ; 0 . 0 ] ; | [ | 21863 . 0 ; 32794 . 0 ; 26934 . 0 ; 10931 . 0 ; 26250 . 0 ; 43725 . 0 ; 53867 . 0 ; 28939 . 0 ; 0 . 0 ] ; | [ | 16002 . 0 ; 21863 . 0 ; 32004 . 0 ; 10931 . 0 ; 14529 . 0 ; 16368 . 0 ; 15318 . 0 ; 32794 . 0 ; 0 . 0 ] ; | [ | 6345 . 0 ; 7818 . 0 ; 15636 . 0 ; 7077 . 0 ; 8184 . 0 ; 14163 . 0 ; 1107 . 0 ; 4872 . 0 ; 0 . 0 ] ; | [ | 1760 . 0 ; 1454 . 0 ; 1167 . 0 ; 880 . 0 ; 287 . 0 ; 2640 . 0 ; 19 . 0 ; 2047 . 0 ; 1454 . 0 ] ; | [ | 574 . 0 ; 0 . 0 ; 880 . 0 ; 287 . 0 ; 19 . 0 ; 1760 . 0 ; 1167 . 0 ; 306 . 0 ; 574 . 0 ] ; | [ | 204 . 0 ; 0 . 0 ; 177 . 0 ; 1265 . 0 ; 4 . 0 ; 385 . 0 ; 200 . 0 ; 208 . 0 ; 204 . 0 ] ; | [ | 0 . 0 ; 102 . 0 ; 106 . 0 ; 4 . 0 ; 98 . 0 ; 1367 . 0 ; 487 . 0 ; 204 . 0 ; 0 . 0 ] | ] | [ | [ | 4 . 0 ; - 13 . 0 ; 11 . 0 ; - 9 . 0 ; - 9 . 0 ; - 3 . 0 ; - 1 . 0 ; 4 . 0 ; 0 . 0 ] ; | [ | - 156 . 0 ; 59 . 0 ; - 42 . 0 ; 6 . 0 ; 19 . 0 ; - 20 . 0 ; - 10 . 0 ; - 12 . 0 ; 0 . 0 ] ; | [ | 64 . 0 ; - 152 . 0 ; 62 . 0 ; - 8 . 0 ; 32 . 0 ; - 41 . 0 ; 19 . 0 ; - 11 . 0 ; 0 . 0 ] ; | [ | 124 . 0 ; 621 . 0 ; - 145 . 0 ; 208 . 0 ; 54 . 0 ; - 57 . 0 ; 30 . 0 ; 15 . 0 ; 0 . 0 ] ; | [ | - 23437 . 0 ; - 2634 . 0 ; 6601 . 0 ; 6259 . 0 ; - 1507 . 0 ; - 1821 . 0 ; 2620 . 0 ; - 2115 . 0 ; - 1489 . 0 ] ; | [ | 62911 . 0 ; - 119919 . 0 ; 79336 . 0 ; 17814 . 0 ; - 24241 . 0 ; 12068 . 0 ; 8306 . 0 ; - 4893 . 0 ; 8902 . 0 ] ; | [ | 389061 . 0 ; - 262125 . 0 ; - 44088 . 0 ; 8387 . 0 ; - 22976 . 0 ; - 2093 . 0 ; - 615 . 0 ; - 9720 . 0 ; 6633 . 0 ] ; | [ | - 412235 . 0 ; - 157046 . 0 ; - 31430 . 0 ; 37817 . 0 ; - 9740 . 0 ; - 13 . 0 ; - 7449 . 0 ; 9644 . 0 ; 0 . 0 ] | ] | [ | [ | - 29 . 0 ; - 1 . 0 ; 9 . 0 ; 6 . 0 ; - 6 . 0 ; 5 . 0 ; 4 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | - 48 . 0 ; - 125 . 0 ; - 26 . 0 ; - 37 . 0 ; 18 . 0 ; - 13 . 0 ; - 20 . 0 ; - 2 . 0 ; 0 . 0 ] ; | [ | - 150 . 0 ; - 46 . 0 ; 68 . 0 ; 54 . 0 ; 14 . 0 ; 24 . 0 ; - 28 . 0 ; 22 . 0 ; 0 . 0 ] ; | [ | - 621 . 0 ; 532 . 0 ; - 694 . 0 ; - 20 . 0 ; 192 . 0 ; - 94 . 0 ; 71 . 0 ; - 73 . 0 ; 0 . 0 ] ; | [ | - 14614 . 0 ; - 19828 . 0 ; - 5869 . 0 ; 1881 . 0 ; - 4372 . 0 ; - 2255 . 0 ; 782 . 0 ; 930 . 0 ; 913 . 0 ] ; | [ | 139737 . 0 ; 0 . 0 ; 24667 . 0 ; 51123 . 0 ; - 5102 . 0 ; 7429 . 0 ; - 4095 . 0 ; - 1976 . 0 ; - 9566 . 0 ] ; | [ | - 138081 . 0 ; 0 . 0 ; 37205 . 0 ; - 49039 . 0 ; - 41901 . 0 ; - 33872 . 0 ; - 27037 . 0 ; - 12474 . 0 ; 18797 . 0 ] ; | [ | 0 . 0 ; 28492 . 0 ; 133236 . 0 ; 69654 . 0 ; 52322 . 0 ; - 49577 . 0 ; - 26430 . 0 ; - 3593 . 0 ; 0 . 0 ] | ] | [ | [ | 3086 . 0 ; 15746 . 0 ; 69613 . 0 ; 59899 . 0 ; 75645 . 0 ; 88306 . 0 ; 12661 . 0 ; 2658 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 21863 . 0 ; 32794 . 0 ; 10931 . 0 ; 73 . 0 ; 4387 . 0 ; 26934 . 0 ; 1473 . 0 ; 2157 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 10 . 0 ; 16002 . 0 ; 21863 . 0 ; 10931 . 0 ; 1473 . 0 ; 32004 . 0 ; 4387 . 0 ; 73 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 10 . 0 ; 6345 . 0 ; 7818 . 0 ; 1107 . 0 ; 15636 . 0 ; 7077 . 0 ; 8184 . 0 ; 532 . 0 ; 10 . 0 ; 0 . 0 ] ; | [ | 19 . 0 ; 1760 . 0 ; 1454 . 0 ; 287 . 0 ; 1167 . 0 ; 880 . 0 ; 574 . 0 ; 2640 . 0 ; 19 . 0 ; 1454 . 0 ] ; | [ | 19 . 0 ; 574 . 0 ; 287 . 0 ; 306 . 0 ; 1760 . 0 ; 12 . 0 ; 31 . 0 ; 38 . 0 ; 19 . 0 ; 574 . 0 ] ; | [ | 4 . 0 ; 204 . 0 ; 177 . 0 ; 8 . 0 ; 31 . 0 ; 200 . 0 ; 1265 . 0 ; 102 . 0 ; 4 . 0 ; 204 . 0 ] ; | [ | 4 . 0 ; 102 . 0 ; 106 . 0 ; 8 . 0 ; 98 . 0 ; 1367 . 0 ; 487 . 0 ; 204 . 0 ; 4 . 0 ; 102 . 0 ] | ] | [ | [ | 21 . 0 ; - 95 . 0 ; - 157 . 0 ; 41 . 0 ; - 5 . 0 ; 42 . 0 ; 23 . 0 ; 30 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | - 160 . 0 ; - 313 . 0 ; - 235 . 0 ; 60 . 0 ; - 74 . 0 ; - 76 . 0 ; - 27 . 0 ; 34 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | - 325 . 0 ; - 322 . 0 ; - 79 . 0 ; 232 . 0 ; - 52 . 0 ; 97 . 0 ; 55 . 0 ; - 41 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 2268 . 0 ; - 979 . 0 ; 802 . 0 ; 602 . 0 ; - 668 . 0 ; - 33 . 0 ; 345 . 0 ; 201 . 0 ; - 55 . 0 ; 0 . 0 ] ; | [ | 7610 . 0 ; - 4997 . 0 ; - 7689 . 0 ; - 5841 . 0 ; - 2617 . 0 ; 1115 . 0 ; - 748 . 0 ; - 607 . 0 ; 6074 . 0 ; 354 . 0 ] ; | [ | - 18549 . 0 ; 30125 . 0 ; 20012 . 0 ; - 730 . 0 ; 824 . 0 ; 23 . 0 ; 1289 . 0 ; - 352 . 0 ; - 14767 . 0 ; - 2062 . 0 ] ; | [ | - 135245 . 0 ; - 14594 . 0 ; 4197 . 0 ; - 4030 . 0 ; - 5630 . 0 ; - 2898 . 0 ; 2540 . 0 ; - 306 . 0 ; 2939 . 0 ; 1986 . 0 ] ; | [ | 89948 . 0 ; 2103 . 0 ; 8963 . 0 ; 2695 . 0 ; 3682 . 0 ; 1648 . 0 ; 866 . 0 ; - 154 . 0 ; - 1963 . 0 ; - 283 . 0 ] | ] | [ | [ | - 342 . 0 ; 136 . 0 ; - 23 . 0 ; 62 . 0 ; 66 . 0 ; - 52 . 0 ; - 33 . 0 ; 17 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 524 . 0 ; - 149 . 0 ; - 35 . 0 ; 117 . 0 ; 151 . 0 ; 122 . 0 ; - 71 . 0 ; - 62 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | - 105 . 0 ; - 137 . 0 ; 258 . 0 ; 35 . 0 ; - 116 . 0 ; - 88 . 0 ; - 112 . 0 ; - 80 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 854 . 0 ; - 205 . 0 ; - 936 . 0 ; - 240 . 0 ; 140 . 0 ; - 341 . 0 ; - 97 . 0 ; - 232 . 0 ; 536 . 0 ; 0 . 0 ] ; | [ | - 56980 . 0 ; 8016 . 0 ; 1012 . 0 ; 1448 . 0 ; - 3024 . 0 ; - 3710 . 0 ; 318 . 0 ; 503 . 0 ; 3767 . 0 ; 577 . 0 ] ; | [ | 138606 . 0 ; - 13478 . 0 ; - 4964 . 0 ; 1441 . 0 ; - 1319 . 0 ; - 1482 . 0 ; 427 . 0 ; 1236 . 0 ; - 9167 . 0 ; - 1918 . 0 ] ; | [ | 71234 . 0 ; - 41116 . 0 ; 5334 . 0 ; - 4935 . 0 ; - 1848 . 0 ; 66 . 0 ; 434 . 0 ; - 1748 . 0 ; 3780 . 0 ; - 701 . 0 ] ; | [ | - 47645 . 0 ; 11647 . 0 ; 2166 . 0 ; 3194 . 0 ; 679 . 0 ; 0 . 0 ; - 244 . 0 ; - 419 . 0 ; - 2531 . 0 ; 48 . 0 ] | ] |
let anpm a = let w = mod_float a twopi in if abs_float w >= pic then begin if a < 0 . 0 then else end else w
let planetpv epoch np pv = let t = ( ( epoch . ( 0 ) . - j2000 ) . + epoch . ( 1 ) ) . / jmillenia in let da = ref ( a . ( np ) . ( 0 ) . + ( a . ( np ) . ( 1 ) . + a . ( np ) . ( 2 ) . * t ) . * t ) and dl = ref ( ( 3600 . 0 . * dlm . ( np ) . ( 0 ) . + ( dlm . ( np ) . ( 1 ) . + dlm . ( np ) . ( 2 ) . * t ) . * t ) . * a2r ) and de = e . ( np ) . ( 0 ) . + ( e . ( np ) . ( 1 ) . + e . ( np ) . ( 2 ) . * t ) . * t and dp = anpm ( ( 3600 . 0 . * pi . ( np ) . ( 0 ) . + ( pi . ( np ) . ( 1 ) . + pi . ( np ) . ( 2 ) . * t ) . * t ) . * a2r ) and di = ( 3600 . 0 . * dinc . ( np ) . ( 0 ) . + ( dinc . ( np ) . ( 1 ) . + dinc . ( np ) . ( 2 ) . * t ) . * t ) . * a2r and doh = anpm ( ( 3600 . 0 . * omega . ( np ) . ( 0 ) . + ( omega . ( np ) . ( 1 ) . + omega . ( np ) . ( 2 ) . * t ) . * t ) . * a2r ) and dmu = 0 . 35953620 . * t in let kp = kp . ( np ) and kq = kq . ( np ) and ca = ca . ( np ) and sa = sa . ( np ) and cl = cl . ( np ) and sl = sl . ( np ) in for k = 0 to 7 do let arga = kp . ( k ) . * dmu and argl = kq . ( k ) . * dmu in da := ! da . + ( ca . ( k ) . * cos arga . + sa . ( k ) . * sin arga ) . * 0 . 0000001 ; dl := ! dl . + ( cl . ( k ) . * cos argl . + sl . ( k ) . * sin argl ) . * 0 . 0000001 done ; begin let arga = kp . ( 8 ) . * dmu in da := ! da . + t . * ( ca . ( 8 ) . * cos arga . + sa . ( 8 ) . * sin arga ) . * 0 . 0000001 ; for k = 8 to 9 do let argl = kq . ( k ) . * dmu in dl := ! dl . + t . * ( cl . ( k ) . * cos argl . + sl . ( k ) . * sin argl ) . * 0 . 0000001 done ; end ; dl := mod_float ! dl twopi ; let am = ! dl . - dp in let ae = ref ( am . + de . * sin am ) and k = ref 0 in let dae = ref ( ( am . - ! ae . + de . * sin ! ae ) . / ( 1 . 0 . - de . * cos ! ae ) ) in ae := ! ae . + ! dae ; incr k ; while ! k < 10 or abs_float ! dae >= 1e - 12 do done ; let ae2 = ! ae . / 2 . 0 in let at = 2 . 0 . * atan2 ( sqrt ( ( 1 . 0 . + de ) . / ( 1 . 0 . - de ) ) . * sin ae2 ) ( cos ae2 ) and r = ! da . * ( 1 . 0 . - de . * cos ! ae ) and v = gaussk . * sqrt ( ( 1 . 0 . + 1 . 0 . / amas . ( np ) ) . / ( ! da . * ! da . * ! da ) ) and si2 = sin ( di . / 2 . 0 ) in let xq = si2 . * cos doh and xp = si2 . * sin doh and tl = at . + dp in let xsw = sin tl and xcw = cos tl in let xm2 = 2 . 0 . * ( xp . * xcw . - xq . * xsw ) and xf = ! da . / sqrt ( 1 . 0 . - de . * de ) and ci2 = cos ( di . / 2 . 0 ) in let xms = ( de . * sin dp . + xsw ) . * xf and xmc = ( de . * cos dp . + xcw ) . * xf and xpxq2 = 2 . 0 . * xp . * xq in let x = r . * ( xcw . - xm2 . * xp ) and y = r . * ( xsw . + xm2 . * xq ) and z = r . * ( . - xm2 . * ci2 ) in
let radecdist state rdd = rdd . ( 2 ) <- sqrt ( state . ( 0 ) . ( 0 ) . * state . ( 0 ) . ( 0 ) rdd . ( 0 ) <- atan2 state . ( 0 ) . ( 1 ) state . ( 0 ) . ( 0 ) . * r2h ; if rdd . ( 0 ) < 0 . 0 then rdd . ( 0 ) <- rdd . ( 0 ) . + 24 . 0 ; rdd . ( 1 ) <- asin ( state . ( 0 ) . ( 2 ) . / rdd . ( 2 ) ) . * r2d
let _ = let jd = [ | 0 . 0 ; 0 . 0 ] | and pv = [ | [ | 0 . 0 ; 0 . 0 ; 0 . 0 ] ; | [ | 0 . 0 ; 0 . 0 ; 0 . 0 ] | ] | and position = [ | 0 . 0 ; 0 . 0 ; 0 . 0 ] | in jd . ( 0 ) <- j2000 ; jd . ( 1 ) <- 1 . 0 ; for p = 0 to 7 do planetpv jd p pv ; radecdist pv position ; Printf . printf " % d . % 2f . % 2f \ n " %! p position . ( 0 ) position . ( 1 ) done ; for i = 0 to test_loops - 1 do jd . ( 0 ) <- j2000 ; jd . ( 1 ) <- 0 . 0 ; for n = 0 to test_length - 1 do jd . ( 0 ) <- jd . ( 0 ) . + 1 . 0 ; for p = 0 to 7 do done done done
let ( ==: ) = Signal . ( ==: )
module Variable = struct module Internal = struct type t = { assigns_to_wire : Signal . t ; default : Signal . t } end type internal = Internal . t type t = { value : Signal . t ; internal : Internal . t } [ @@ deriving fields ] let sexp_of_t { value ; internal = _ } = [ % message " " ~ _ ( : value : Signal . t ) ] let uid t = Signal . uid t . internal . assigns_to_wire let compare t1 t2 = Signal . Uid . compare ( uid t1 ) ( uid t2 ) let equal = [ % compare . equal : t ] include ( val Comparator . make ~ compare ~ sexp_of_t ) let wire ~ default = let wire = Signal . wire ( Signal . width default ) in { value = wire ; internal = { assigns_to_wire = wire ; default } } ; ; let reg ? enable ~ width spec = let wire = Signal . wire width in let reg = Signal . reg spec ? enable wire in { value = reg ; internal = { assigns_to_wire = wire ; default = reg } } ; ; let pipeline ? enable ~ width ~ depth ( spec : Reg_spec . t ) = if depth = 0 then if Signal . is_empty spec . reg_reset_value then wire ~ default ( : Signal . zero width ) else wire ~ default : spec . reg_reset_value else ( let r = reg spec ? enable ~ width in { r with value = Signal . pipeline ~ n ( : depth - 1 ) spec ? enable r . value } ) ; ; end
type t = | Assign of Variable . t * Signal . t | If of Signal . t * t list * t list | Switch of Signal . t * ( Signal . t * t list ) list
type always = t [ @@ deriving sexp_of ]
type ' a case = ' a * t list [ @@ deriving sexp_of ]
type ' a cases = ' a case list [ @@ deriving sexp_of ]
let if_ sel on_true on_false = If ( sel , on_true , on_false )
let elif c t f = [ if_ c t f ]
let when_ sel on_true = if_ sel on_true [ ]
let unless sel on_false = if_ sel [ ] on_false
let switch sel cases = Switch ( sel , cases )
let proc s = if_ Signal . vdd s [ ]
let ( <-- ) ( a : Variable . t ) b = if Signal . width a . value <> Signal . width b then raise_s [ % message " attempt to assign expression to [ Always . variable ] of different width " ~ variable_name ( : Signal . names a . value : string list ) ~ guared_variable_width ( : Signal . width a . value : int ) ~ expression_width ( : Signal . width b : int ) ~ expression ( : b : Signal . t ) ] ; Assign ( a , b ) ; ;
let ( . <-- ) ( a : Variable . t ) b = a <-- Signal . of_int ~ width ( : Signal . width a . value ) b
let list_of_set s = Set . fold s ~ init [ ] : ~ f ( : fun l e -> e :: l )
let rec find_targets set statements = List . fold statements ~ init : set ~ f ( : fun set statement -> match statement with | Assign ( variable , _ ) -> Set . add set variable | If ( _ , t , f ) -> let set = find_targets set t in find_targets set f | Switch ( _ , cases ) -> List . fold cases ~ init : set ~ f ( : fun set case -> find_targets set ( snd case ) ) ) ; ;
let filter_by_target variable statements = let rec loops statements = List . filter_map statements ~ f : loop and loop statement = match statement with | Assign ( v , _ ) -> if Variable . equal v variable then Some statement else None | If ( s , t , f ) -> let t = loops t in let f = loops f in if List . is_empty t && List . is_empty f then None else Some ( If ( s , t , f ) ) | Switch ( sel , cases ) -> let cases = List . filter_map cases ~ f ( : fun ( m , c ) -> let c = loops c in if List . is_empty c then None else Some ( m , c ) ) in if List . is_empty cases then None else Some ( Switch ( sel , cases ) ) in loops statements ; ;
let rec compile_mux statements ~ default = match statements with | [ ] -> default | statement :: statements -> let default = match statement with | If ( s , t , f ) -> let s = Signal . reduce ~ f : Signal . ( |: ) ( Signal . bits_msb s ) in let t = compile_mux t ~ default in let f = compile_mux f ~ default in Signal . mux s [ f ; t ] | Assign ( _ , d ) -> d | Switch ( sel , cases ) -> let rec build = function | [ ] -> default | ( mtch , case ) :: t -> Signal . mux ( sel ==: mtch ) [ build t ; compile_mux case ~ default ] in build cases in compile_mux statements ~ default ; ;
let compile statements = let targets = list_of_set ( find_targets ( Set . empty ( module Variable ) ) statements ) in List . iter targets ~ f ( : fun target -> let statements = filter_by_target target statements in Signal . ( <== ) target . internal . assigns_to_wire ( compile_mux statements ~ default : target . internal . default ) ) ; ;
module State_machine = struct type ' a t = { current : Signal . t ; is : ' a -> Signal . t ; set_next : ' a -> always ; switch : ? default : always list -> ' a cases -> always } [ @@ deriving sexp_of ] module Encoding = struct type t = | Binary | Gray | Onehot [ @@ deriving sexp_of ] let to_string t = [ % sexp ( t : t ) ] |> Sexp . to_string end module type State = sig type t [ @@ deriving compare , enumerate , sexp_of ] end let create ( ? encoding = Encoding . Binary ) ( ? enable = Signal . vdd ) ( type a ) ( module State : State with type t = a ) reg_spec = let module State = struct include State include Comparator . Make ( State ) end in let nstates = List . length State . all in let ls = if nstates = 1 then 1 else Int . ceil_log2 nstates in let state_bits i = match encoding with | Binary -> Signal . of_int ~ width : ls i | Gray -> Signal . of_bit_string ( Bits . binary_to_gray ( Bits . of_int ~ width : ls i ) |> Bits . to_bstr ) | Onehot -> let nstates ' = if nstates = 1 then 1 else nstates - 1 in Signal . of_bit_string Bits . ( select ( binary_to_onehot ( of_int ~ width : ls i ) ) nstates ' 0 |> to_bstr ) in let states = List . mapi State . all ~ f ( : fun i s -> s , ( i , state_bits i ) ) in let var = match encoding with | Binary | Gray -> Variable . reg reg_spec ~ enable ~ width : ls | Onehot -> Variable . reg { reg_spec with reg_clear_value = Signal . one nstates ; reg_reset_value = Signal . one nstates } ~ enable ~ width : nstates in let find_state name state = match List . Assoc . find states state ~ equal [ :% compare . equal : State . t ] with | Some x -> x | None -> raise_s [ % message ( String . concat [ " [ Always . State_machine . " ; name ; " ] got unknown state " ] ) ~ _ ( : state : State . t ) ] in let state_val name s = snd ( find_state name s ) in let set_next s = var <-- state_val " set_next " s in let current = var . value in let switch ? default cases = let rec unique set = function | [ ] -> set | ( state , _ ) :: tl -> if Set . mem set state then raise_s [ % message " [ Always . State_machine . switch ] got repeated state " ~ _ ( : state : State . t ) ] ; unique ( Set . add set state ) tl in let all_states = Set . of_list ( module State ) State . all in let case_states = unique ( Set . empty ( module State ) ) cases in let unknown_states = Set . diff case_states all_states in if not ( Set . is_empty unknown_states ) then raise_s [ % message " [ Always . State_machine . switch ] got unknown states " ~ _ ( : unknown_states : Set . M ( State ) . t ) ] ; let unhandled_states = Set . diff all_states case_states in let cases = match default with | None -> if not ( Set . is_empty unhandled_states ) then raise_s [ % message " [ Always . State_machine . switch ] without [ ~ default ] had unhandled states " ~ _ ( : unhandled_states : Set . M ( State ) . t ) ] ; cases | Some default -> cases @ ( Set . to_list unhandled_states |> List . map ~ f ( : fun state -> state , default ) ) in match encoding with | Binary | Gray -> switch current ( List . map cases ~ f ( : fun ( s , c ) -> state_val " switch " s , c ) ) | Onehot -> proc ( List . map cases ~ f ( : fun ( s , c ) -> let i , _ = find_state " switch " s in when_ ( Signal . bit current i ) c ) ) in let is s = match encoding with | Binary | Gray -> state_val " is " s ==: current | Onehot -> Signal . bit var . value ( fst ( find_state " is " s ) ) in { current ; is ; set_next ; switch } ; ; end
type dep_type = System | Internal
type dependency_tag = Target of Name . t | Dependency of Libname . t
type cpkg_config = { cpkg_conf_libs : string list ; cpkg_conf_includes : filepath list }
type project_config = { project_dep_data : ( Libname . t , dep_type ) Hashtbl . t ; project_pkgdeps_dag : dependency_tag Dag . t ; project_targets_dag : Name . t Dag . t ; project_all_deps : dependency list ; project_file : Project . t ; project_ocamlcfg : ( string , string ) Hashtbl . t ; project_ocamlmkcfg : ( string , string ) Hashtbl . t ; project_cpkgs : ( string , cpkg_config ) Hashtbl . t }
let get_ocaml_config_key_hashtbl key h = try Hashtbl . find h key with Not_found -> raise ( OcamlConfigMissing key )
let getOcamlConfigKey key = get_ocaml_config_key_hashtbl key ( Prog . getOcamlConfig ( ) )
let get_ocaml_config_key key project = get_ocaml_config_key_hashtbl key project . project_ocamlcfg
let get_pkg_deps target project = let pkgs = Taskdep . linearize project . project_pkgdeps_dag Taskdep . FromParent [ Target target . target_name ] in List . rev ( list_filter_map ( fun pkg -> match pkg with Dependency d -> Some d | Target _ -> None ) pkgs )
let get_c_pkg cname project = try Hashtbl . find project . project_cpkgs cname with Not_found -> failwith ( sprintf " C package % s not found in the hashtbl : internal error " cname )
let is_pkg_internal project pkg = Hashtbl . find project . project_dep_data pkg = Internal