text
stringlengths
0
601k
module Digest = struct type t = Md5_lib . t let sexp_of_t t = [ % sexp_of : string ] ( Md5_lib . to_hex t : string ) let compare a b = String . compare ( Md5_lib . to_binary a ) ( Md5_lib . to_binary b ) let equal a b = String . equal ( Md5_lib . to_binary a ) ( Md5_lib . to_binary b ) let none = Md5_lib . string " non...
type ( ' i , ' o ) t = { in_ports : Port_list . t ; out_ports_before_clock_edge : Port_list . t ; out_ports_after_clock_edge : Port_list . t ; internal_ports : Port_list . t ; inputs : ' i ; outputs_after_clock_edge : ' o ; outputs_before_clock_edge : ' o ; reset : task ; cycle_check : task ; cycle_before_clock_edge : ...
let sexp_of_t sexp_of_i sexp_of_o t = [ % message " " ~ inputs ( : t . inputs : i ) ~ outputs_before_clock_edge ( : t . outputs_before_clock_edge : o ) ~ outputs_after_clock_edge ( : t . outputs_after_clock_edge : o ) ] ; ;
type t_port_list = ( Port_list . t , Port_list . t ) t
module Config = struct type t = { is_internal_port : ( Signal . t -> bool ) option ; combinational_ops_database : Combinational_ops_database . t ; compute_digest : bool ; deduplicate_signals : bool ; store_circuit : bool } let empty_ops_database = Combinational_ops_database . create ( ) let default = { is_internal_port...
module type Private = Cyclesim0_intf . Private
module Private = struct type nonrec ( ' i , ' o ) t = ( ' i , ' o ) t type nonrec port_list = Port_list . t type nonrec t_port_list = t_port_list type nonrec task = task let create ~ in_ports ~ out_ports_before_clock_edge ~ out_ports_after_clock_edge ~ internal_ports ~ reset ~ cycle_check ~ cycle_before_clock_edge ~ cy...
module type Private = sig type ( ' i , ' o ) t type port_list type t_port_list type task = unit -> unit val create : in_ports : port_list -> out_ports_before_clock_edge : port_list -> out_ports_after_clock_edge : port_list -> internal_ports : port_list -> reset : task -> cycle_check : task -> cycle_before_clock_edge : ...
module type Cyclesim0 = sig module Port_list : sig type t = ( string * Bits . t ref ) list [ @@ deriving sexp_of ] end module Digest : sig type t = Md5_lib . t [ @@ deriving sexp_of , compare , equal ] val none : t end type task = unit -> unit type ( ' i , ' o ) t = { in_ports : Port_list . t ; out_ports_before_clock_e...
module Combine_error = struct type t = { cycle_no : int ; clock_edge : Side . t ; port_name : string ; value0 : Bits . t ; value1 : Bits . t } [ @@ deriving sexp_of ] end
let default_on_error error = raise_s [ % message " [ Cyclesim . combine ] output port values differ " ( error : Combine_error . t ) ] ; ;
let combine ( ? port_sets_may_differ = false ) ( ? on_error = default_on_error ) ( s0 : _ t ) ( s1 : _ t ) = let si = Set . to_list ( List . fold ( s0 . in_ports @ s1 . in_ports ) ~ init ( : Set . empty ( module String ) ) ~ f ( : fun s ( n , _ ) -> Set . add s n ) ) in let out_port_set = Set . to_list ( List . fold ( ...
module Mangle_names = struct module IntPair = struct module T = struct type t = Signal . Uid . t * int [ @@ deriving compare , sexp_of ] end include T include Comparator . Make ( T ) end let port_names_map ports = List . fold ports ~ init ( : Map . empty ( module IntPair ) ) ~ f ( : fun map signal -> Map . set map ~ ke...
module Internal_ports = struct let create circuit ~ is_internal_port = let name = Staged . unstage ( Mangle_names . create [ ] " _ " circuit ) in let i = match is_internal_port with | None -> [ ] | Some f -> Signal_graph . filter ( Circuit . signal_graph circuit ) ~ f ( : fun s -> ( not ( Circuit . is_input circuit s )...
module Maps : sig type t val create : Schedule . t -> t val find_data_by_uid_exn : t -> Signal . Uid . t -> Bits . Mutable . t val find_reg_by_uid_exn : t -> Signal . Uid . t -> Bits . Mutable . t val find_data_exn : t -> Signal . t -> Bits . Mutable . t val find_data : t -> Signal . t -> Bits . Mutable . t val find_re...
module Io_ports = struct type ' a t = { in_ports : ' a ; out_ports : ' a ; internal_ports : ' a } let create circuit maps internal_ports : _ t = let in_ports = List . map ( Circuit . inputs circuit ) ~ f ( : fun signal -> List . hd_exn ( names signal ) , Maps . find_data_exn maps signal ) in let out_ports = List . map ...
module Compile = struct let mutable_to_int x = Int64 . to_int_trunc ( Bits . Mutable . unsafe_get_int64 x 0 ) let zero n = Bits . Mutable . of_constant ( Constant . of_int ~ width : n 0 ) let dispach_on_width width ~ less_than_64 ~ exactly_64 ~ more_than_64 = Some ( if width <= 64 then less_than_64 else if width = 64 t...
module Last_layer = struct let create ~ schedule circuit tasks_comb = let nodes = Signal_graph . last_layer_of_nodes ~ is_input ( : Circuit . is_input circuit ) ( Circuit . signal_graph circuit ) |> List . filter ~ f ( : fun uid -> not ( Schedule . is_alias schedule uid ) ) |> Set . of_list ( module Signal . Uid ) in l...
module Compute_digest = struct let create out_ports_before out_ports_after internal_ports = let digest = ref ( Md5_lib . string " digest of hardcaml simulation " ) in let digest_length = String . length ( Md5_lib . to_binary ! digest ) in let ports = [ out_ports_before ; out_ports_after ; internal_ports ] |> List . con...
let create ( ? config = Cyclesim0 . Config . default ) circuit = let circuit = if config . deduplicate_signals then Dedup . deduplicate circuit else circuit in let assertions = Circuit . assertions circuit in let internal_ports = Internal_ports . create circuit ~ is_internal_port : config . is_internal_port in let bund...
module Width = struct type t = | W32 | W64 [ @@ deriving sexp_of ] let num_bits = function | W32 -> 32 | W64 -> 64 ; ; end
let op2 op name ( width : Width . t ) = let create_fn i o = match i , o with | [ x ; y ] , [ z ] -> let x = Bits . Mutable . unsafe_get_int64 x 0 in let y = Bits . Mutable . unsafe_get_int64 y 0 in ( match width with | W32 -> let x = Int32 . of_int64_trunc x |> Int32 . float_of_bits in let y = Int32 . of_int64_trunc y ...
let op1 op name ( width : Width . t ) = let create_fn i o = match i , o with | [ x ] , [ z ] -> let x = Bits . Mutable . unsafe_get_int64 x 0 in ( match width with | W32 -> let x = Int32 . of_int64_trunc x |> Int32 . float_of_bits in let z ' = op x |> Int32 . bits_of_float |> Int64 . of_int32_exn in Bits . Mutable . un...
module F ( P : sig val width : Width . t end ) = struct let database = Combinational_ops_database . create ( ) let n s = String . concat ~ sep " : _ " [ " float " ; Int . to_string ( Width . num_bits P . width ) ; s ] let get_output = function | [ a ] -> a | _ -> raise_s [ % message " Floating point operation did not r...
module Float = F ( struct let width = Width . W32 end )
module Double = F ( struct let width = Width . W64 end )
module type Cyclesim = sig module Port_list = Cyclesim0 . Port_list module Digest = Cyclesim0 . Digest module Config = Cyclesim0 . Config type ( ' i , ' o ) t type t_port_list = ( Port_list . t , Port_list . t ) t val circuit : _ t -> Circuit . t option val cycle : _ t -> unit val cycle_check : _ t -> unit val cycle_be...
type t = { schedule : Signal . t list ; regs : Signal . t list ; mems : Signal . t list ; consts : Signal . t list ; inputs : Signal . t list ; aliases : Signal . t Hashtbl . M ( Signal . Uid ) . t }
let find_elements circuit = Signal_graph . depth_first_search ( Circuit . signal_graph circuit ) ~ init ( [ ] , : [ ] , [ ] , [ ] , [ ] ) ~ f_before ( : fun ( regs , mems , consts , inputs , comb_signals ) signal -> if Signal . is_empty signal then regs , mems , consts , inputs , comb_signals else if Signal . is_reg si...
let rec unwrap_signal ( signal : Signal . t ) = match signal with | Wire { driver ; _ } -> if Signal . is_empty ! driver then None else ( match unwrap_signal ! driver with | Some _ as ret -> ret | None -> Some ! driver ) | _ -> Some signal ; ;
let unwrap_wire signal = match signal with | Signal . Wire _ -> unwrap_signal signal | _ -> None ; ;
let create_aliases signal_graph = let table = Hashtbl . create ( module Signal . Uid ) in Signal_graph . iter signal_graph ~ f ( : fun signal -> match unwrap_wire signal with | None -> ( ) | Some unwrapped -> Hashtbl . set ~ key ( : Signal . uid signal ) ~ data : unwrapped table ) ; table ; ;
let resolve_alias ( t : t ) uid = Hashtbl . find t . aliases uid
let is_alias ( t : t ) uid = Hashtbl . mem t . aliases uid
let scheduling_deps ( s : Signal . t ) = Signal_graph . scheduling_deps s
let create circuit internal_ports = let regs , mems , consts , inputs , comb_signals = find_elements circuit in let outputs = Circuit . outputs circuit @ internal_ports in let signal_graph = Signal_graph . create outputs in let aliases = create_aliases signal_graph in let schedule = if List . is_empty outputs then [ ] ...
type ' a dagnode = { mutable parents : ' a list ; mutable children : ' a list }
type ' a t = { nodes : ( ' a , ' a dagnode ) Hashtbl . t }
let init ( ) = { nodes = Hashtbl . create 16 }
let length dag = Hashtbl . length dag . nodes
let addEdge a b dag = let maNode = try Some ( Hashtbl . find dag . nodes a ) with Not_found -> None in let mbNode = try Some ( Hashtbl . find dag . nodes b ) with Not_found -> None in ( match ( maNode , mbNode ) with | None , None -> Hashtbl . add dag . nodes a { parents = [ ] ; children = [ b ] } ; Hashtbl . add dag ....
let addNode a dag = try let _ = Hashtbl . find dag . nodes a in ( ) with Not_found -> Hashtbl . add dag . nodes a { parents = [ ] ; children = [ ] }
let addNode_exclusive a dag = try let _ = Hashtbl . find dag . nodes a in raise DagNode_Already_Exists with Not_found -> Hashtbl . add dag . nodes a { parents = [ ] ; children = [ ] }
let hasEdge a b dag = let maNode = try Some ( Hashtbl . find dag . nodes a ) with Not_found -> None in let mbNode = try Some ( Hashtbl . find dag . nodes b ) with Not_found -> None in match ( maNode , mbNode ) with | Some aNode , Some bNode -> List . mem b aNode . children && List . mem a bNode . parents | _ -> false
let delEdge a b dag = let maNode = try Some ( Hashtbl . find dag . nodes a ) with Not_found -> None in let mbNode = try Some ( Hashtbl . find dag . nodes b ) with Not_found -> None in ( match ( maNode , mbNode ) with | Some aNode , Some bNode -> aNode . children <- List . filter ( fun x -> x <> b ) aNode . children ; b...
let addEdges l dag = List . iter ( fun ( n1 , n2 ) -> addEdge n1 n2 dag ) l
let addEdgesConnected l dag = let rec loop parent nodes = match nodes with | [ ] -> ( ) | n :: ns -> addEdge parent n dag ; loop n ns in match l with | [ ] -> ( ) | x [ ] :: -> addNode x dag | x :: l -> loop x l
let addChildrenEdges p l dag = List . iter ( fun x -> addEdge p x dag ) l
let existsNode a dag = Hashtbl . mem dag . nodes a
let getLeaves dag = Hashtbl . fold ( fun k v acc -> if v . children = [ ] then k :: acc else acc ) dag . nodes [ ]
let getRoots dag = Hashtbl . fold ( fun k v acc -> if v . parents = [ ] then k :: acc else acc ) dag . nodes [ ]
let getNode dag a = try Hashtbl . find dag . nodes a with Not_found -> raise DagNode_Not_found
let getNodes dag = Hashtbl . fold ( fun k _ acc -> k :: acc ) dag . nodes [ ]
let getChildren dag a = ( getNode dag a ) . children
let getParents dag a = ( getNode dag a ) . parents
let rec getChildren_full dag a = let children = getChildren dag a in children @ List . concat ( List . map ( getChildren_full dag ) children )
let isChildren dag a b = List . mem b ( getChildren dag a )
let rec isChildren_full dag a b = let children = getChildren dag a in List . mem b children || List . fold_left ( fun acc child -> acc || isChildren_full dag child b ) false children
let subset dag roots = let subdag = init ( ) in let rec loop node = addNode node subdag ; let children = getChildren dag node in List . iter ( fun child -> addEdge node child subdag ; loop child ) children in List . iter ( fun root -> loop root ) roots ; subdag
let copy dag = let nodes = Hashtbl . fold ( fun k _ acc -> k :: acc ) dag . nodes [ ] in let dag2 = init ( ) in let copy_node node = addNode node dag2 ; let children = getChildren dag node in addChildrenEdges node children dag2 in List . iter ( fun node -> copy_node node ) nodes ; dag2
let merge dest src = let nodes = Hashtbl . fold ( fun k _ acc -> k :: acc ) src . nodes [ ] in let dups = ref [ ] in List . iter ( fun node -> if existsNode node dest then dups := node :: ! dups ) nodes ; let copy_node node = addNode node dest ; let children = getChildren src node in addChildrenEdges node children dest...
let transitive_reduction dag = let reducedDag = copy dag in let nodes = Hashtbl . fold ( fun k _ acc -> k :: acc ) dag . nodes [ ] in List . iter ( fun x -> List . iter ( fun y -> List . iter ( fun z -> if hasEdge x y dag && hasEdge y z dag then delEdge x z reducedDag else ( ) ) nodes ) nodes ) nodes ; reducedDag
let dump a_to_string dag = let all = getNodes dag in List . iter ( fun n -> printf " % s :\ n " ( a_to_string n ) ; printf " | parents = % s \ n " ( String . concat " , " ( List . map a_to_string ( getParents dag n ) ) ) ; printf " | children = % s \ n " ( String . concat " , " ( List . map a_to_string ( getChildren da...
let toDot a_to_string name fromLeaf dag = let buf = Buffer . create 1024 in let nodes = getNodes dag in let dotIndex = Hashtbl . create ( List . length nodes ) in let append = Buffer . add_string buf in let sanitizeName = bytes_of_string name in for i = 0 to String . length name - 1 do if ( bytes_get sanitizeName i ) =...
let constructor_descrs ty_res cstrs priv = let num_consts = ref 0 and num_nonconsts = ref 0 in List . iter ( function ( name , [ ] ) -> incr num_consts | ( name , _ ) -> incr num_nonconsts ) cstrs ; let rec describe_constructors idx_const idx_nonconst = function [ ] -> [ ] | ( name , ty_args ) :: rem -> let ( tag , des...
let exception_descr path_exc decl = { cstr_res = Predef . type_exn ; cstr_args = decl ; cstr_arity = List . length decl ; cstr_tag = Cstr_exception path_exc ; cstr_consts = - 1 ; cstr_nonconsts = - 1 ; cstr_private = Public }
let none = { desc = Ttuple [ ] ; level = - 1 ; id = - 1 }
let dummy_label = { lbl_name = " " ; lbl_res = none ; lbl_arg = none ; lbl_mut = Immutable ; lbl_pos = ( - 1 ) ; lbl_all = [ ] ; || lbl_repres = Record_regular ; lbl_private = Public }
let label_descrs ty_res lbls repres priv = let all_labels = Array . create ( List . length lbls ) dummy_label in let rec describe_labels num = function [ ] -> [ ] | ( name , mut_flag , ty_arg ) :: rest -> let lbl = { lbl_name = name ; lbl_res = ty_res ; lbl_arg = ty_arg ; lbl_mut = mut_flag ; lbl_pos = num ; lbl_all = ...
let rec find_constr tag num_const num_nonconst = function [ ] -> raise Constr_not_found | ( name , [ ] as cstr ) :: rem -> if tag = Cstr_constant num_const then cstr else find_constr tag ( num_const + 1 ) num_nonconst rem | ( name , _ as cstr ) :: rem -> if tag = Cstr_block num_nonconst then cstr else find_constr tag n...
let find_constr_by_tag tag cstrlist = find_constr tag 0 0 cstrlist
module Reachable_code_ids = struct type t = { live_code_ids : Code_id . Set . t ; ancestors_of_live_code_ids : Code_id . Set . t } let [ @ ocamlformat " disable " ] print ppf { live_code_ids ; ancestors_of_live_code_ids ; } = Format . fprintf ppf " [ @< hov 1 ( >\ [ @< hov 1 ( > live_code_ids @ % a ) ] @@ \ [ @< hov 1 ...
type elt = { continuation : Continuation . t ; params : Variable . t list ; used_in_handler : Name_occurrences . t ; apply_result_conts : Continuation . Set . t ; bindings : Name_occurrences . t Name . Map . t ; code_ids : Name_occurrences . t Code_id . Map . t ; value_slots : Name_occurrences . t Name . Map . t Value_...
type t = { stack : elt list ; map : elt Continuation . Map . t ; extra : Continuation_extra_params_and_args . t Continuation . Map . t }
type result = { required_names : Name . Set . t ; reachable_code_ids : Reachable_code_ids . t }
let print_elt ppf { continuation ; params ; used_in_handler ; apply_result_conts ; bindings ; code_ids ; value_slots ; apply_cont_args } = Format . fprintf ppf " [ @< hov 1 ( [ >@< hov 1 ( > continuation % a ) ] @@ [ @< hov 1 ( > params % a ) ] @@ [ @< hov \ 1 ( > used_in_handler % a ) ] @@ [ @< hov 1 ( > apply_result_...
let print_stack ppf stack = Format . fprintf ppf " [ @< v 1 ( >% a ) ] " @ ( Format . pp_print_list print_elt ~ pp_sep : Format . pp_print_space ) stack
let print_map ppf map = Continuation . Map . print print_elt ppf map
let print_extra ppf extra = Continuation . Map . print Continuation_extra_params_and_args . print ppf extra
let [ @ ocamlformat " disable " ] print ppf { stack ; map ; extra } = Format . fprintf ppf " [ @< hov 1 ( >\ [ @< hov 1 ( > stack % a ) ] @@ \ [ @< hov 1 ( > map % a ) ] @@ \ [ @< hov 1 ( > extra % a ) ] @\ ) ] " @ print_stack stack print_map map print_extra extra
let _print_result ppf { required_names ; reachable_code_ids } = Format . fprintf ppf " [ @< hov 1 ( [ >@< hov 1 ( > required_names @ % a ) ] @@ [ @< hov 1 ( > reachable_code_ids @ \ % a ) ] ) ] " @@ Name . Set . print required_names Reachable_code_ids . print reachable_code_ids
let empty = { stack = [ ] ; map = Continuation . Map . empty ; extra = Continuation . Map . empty }
let add_extra_params_and_args cont extra t = let extra = Continuation . Map . update cont ( function | Some _ -> Misc . fatal_errorf " Continuation extended a second time " | None -> Some extra ) t . extra in { t with extra }
let enter_continuation continuation params t = let elt = { continuation ; params ; bindings = Name . Map . empty ; code_ids = Code_id . Map . empty ; value_slots = Value_slot . Map . empty ; used_in_handler = Name_occurrences . empty ; apply_cont_args = Continuation . Map . empty ; apply_result_conts = Continuation . S...
let init_toplevel continuation params _t = enter_continuation continuation params empty
let exit_continuation cont t = match t . stack with | [ ] -> Misc . fatal_errorf " Empty stack of variable uses " | ( { continuation ; _ } as elt ) :: stack -> assert ( Continuation . equal cont continuation ) ; let map = Continuation . Map . add cont elt t . map in { t with stack ; map }
let update_top_of_stack ~ t ~ f = match t . stack with | [ ] -> Misc . fatal_errorf " Empty stack of variable uses " | elt :: stack -> { t with stack = f elt :: stack }
let record_var_binding var name_occurrences ~ generate_phantom_lets t = update_top_of_stack ~ t ~ f ( : fun elt -> let bindings = Name . Map . update ( Name . var var ) ( function | None -> Some name_occurrences | Some _ -> Misc . fatal_errorf " The following variable has been bound twice : % a " Variable . print var )...
let record_symbol_projection var name_occurrences t = update_top_of_stack ~ t ~ f ( : fun elt -> let bindings = Name . Map . update ( Name . var var ) ( function | None -> Some name_occurrences | Some prior_occurences as original -> if Name_occurrences . equal prior_occurences name_occurrences then original else Misc ....
let record_symbol_binding symbol name_occurrences t = update_top_of_stack ~ t ~ f ( : fun elt -> let bindings = Name . Map . update ( Name . symbol symbol ) ( function | None -> Some name_occurrences | Some _ -> Misc . fatal_errorf " The following symbol has been bound twice : % a " Symbol . print symbol ) elt . bindin...
let record_code_id_binding code_id name_occurrences t = update_top_of_stack ~ t ~ f ( : fun elt -> let code_ids = Code_id . Map . update code_id ( function | None -> Some name_occurrences | Some _ -> Misc . fatal_errorf " The following code_id has been bound twice : % a " Code_id . print code_id ) elt . code_ids in { e...
let record_value_slot src value_slot dst t = update_top_of_stack ~ t ~ f ( : fun elt -> let value_slots = Value_slot . Map . update value_slot ( function | None -> Some ( Name . Map . singleton src dst ) | Some map -> Some ( Name . Map . update src ( function | None -> Some dst | Some dst ' -> Some ( Name_occurrences ....
let add_used_in_current_handler name_occurrences t = update_top_of_stack ~ t ~ f ( : fun elt -> let used_in_handler = Name_occurrences . union elt . used_in_handler name_occurrences in { elt with used_in_handler } )
let add_apply_result_cont k t = update_top_of_stack ~ t ~ f ( : fun elt -> let apply_result_conts = Continuation . Set . add k elt . apply_result_conts in { elt with apply_result_conts } )
let add_apply_cont_args cont arg_name_occurrences t = update_top_of_stack ~ t ~ f ( : fun elt -> let apply_cont_args = Continuation . Map . update cont ( fun map_opt -> let map = Option . value ~ default : Numeric_types . Int . Map . empty map_opt in let map , _ = List . fold_left ( fun ( map , i ) name_occurrences -> ...
module Dependency_graph = struct type t = { code_age_relation : Code_age_relation . t ; name_to_name : Name . Set . t Name . Map . t ; name_to_code_id : Code_id . Set . t Name . Map . t ; code_id_to_name : Name . Set . t Code_id . Map . t ; code_id_to_code_id : Code_id . Set . t Code_id . Map . t ; unconditionally_used...
let analyze ~ return_continuation ~ exn_continuation ~ code_age_relation ~ used_value_slots { stack ; map ; extra } = Profile . record_call ~ accumulate : true " data_flow " ( fun ( ) -> assert ( stack = [ ] ) ; let deps = Dependency_graph . create map extra ~ return_continuation ~ exn_continuation ~ code_age_relation ...
let errmsg_to_string = function | Some msg -> msg | None -> " unknown "
let mysql_exec dbhandle cmds = let res = exec dbhandle cmds in match status dbhandle with | StatusError _ -> let msg = errmsg dbhandle |> errmsg_to_string in let err = Printf . sprintf " MySQL Error : % s \ n " msg in failwith err | _ -> res
let strip errmsg = function | Some s -> s | None -> failwith errmsg