text
stringlengths
12
786k
let of_char c = let x = Char . code c in hexa . [ x lsr 4 ] , hexa . [ x land 0xf ]
let to_char x y = let code c = match c with | ' 0 ' . . ' 9 ' -> Char . code c - 48 | ' A ' . . ' F ' -> Char . code c - 55 | ' a ' . . ' f ' -> Char . code c - 87 | _ -> invalid_arg " Hex . to_char : % d is an invalid char " ( Char . code c ) i...
let of_string_fast s = let len = String . length s in let buf = Bytes . create ( len * 2 ) in for i = 0 to len - 1 do Bytes . unsafe_set buf ( i * 2 ) ( String . unsafe_get hexa1 ( Char . code ( String . unsafe_get s i ) ) ) ; Bytes . unsafe_set buf ( succ ( i * 2 ) ...
let of_helper ~ ignore ( next : int -> char ) len = let buf = Buffer . create len in for i = 0 to len - 1 do let c = next i in if List . mem c ignore then ( ) else let x , y = of_char c in Buffer . add_char buf x ; Buffer . add_char buf y ; done ; ` Hex ( Buffer . contents buf )...
let of_string ( ? ignore = [ ] ) s = match ignore with | [ ] -> of_string_fast s | ignore -> of_helper ~ ignore ( fun i -> s . [ i ] ) ( String . length s )
let of_bytes ? ignore b = of_string ? ignore ( Bytes . to_string b )
let to_helper ~ empty_return ~ create ~ set ( ` Hex s ) = if s = " " then empty_return else let n = String . length s in let buf = create ( n / 2 ) in let rec aux i j = if i >= n then ( ) else if j >= n then invalid_arg " Hex conversion : Hex string cannot have an odd number of charac...
let to_bytes hex = to_helper ~ empty_return : Bytes . empty ~ create : Bytes . create ~ set : Bytes . set hex
let to_string hex = Bytes . to_string @@ to_bytes hex
let of_cstruct ( ? ignore [ ] ) = cs = let open Cstruct in of_helper ~ ignore ( fun i -> Bigarray . Array1 . get cs . buffer ( cs . off + i ) ) cs . len
let empty_cstruct = Cstruct . of_string " "
let to_cstruct hex = to_helper ~ empty_return : empty_cstruct ~ create : Cstruct . create ~ set : Cstruct . set_char hex
let of_bigstring ( ? ignore [ ] ) = buf = of_helper ~ ignore ( Bigarray . Array1 . get buf ) ( Bigarray . Array1 . dim buf )
let to_bigstring hex = to_helper ~ empty_return : empty_cstruct . buffer ~ create : Bigarray . ( Array1 . create char c_layout ) ~ set : Bigarray . Array1 . set hex
let hexdump_s ( ? print_row_numbers = true ) ( ? print_chars = true ) ( ` Hex s ) = let char_len = 16 in let hex_len = char_len * 2 in let buf = Buffer . create ( ( String . length s ) * 4 ) in let ( <= ) buf s = Buffer . add_string buf s in let n = String . length s in...
let hexdump ? print_row_numbers ? print_chars hex = Printf . printf " % s " ( hexdump_s ? print_row_numbers ? print_chars hex )
let pp ppf ( ` Hex hex ) = Format . pp_print_string ppf hex
let show ( ` Hex hex ) = hex
type t = Modname . t list
type file_entry = FileEntry of ( filepath * filepath ) | GeneratedFileEntry of ( filepath * filepath * filename ) | DirectoryEntry of ( filepath * filepath )
let file_entry_to_string = function | FileEntry ( p , f ) -> Printf . sprintf " FileEntry % s % s " ( fp_to_string p ) ( fp_to_string f ) | DirectoryEntry ( p , f ) -> Printf . sprintf " DirectoryEntry % s % s " ( fp_to_string p ) ( fp_to_string f ) | GeneratedFileEntry...
let hiers : ( t , file_entry ) Hashtbl . t = Hashtbl . create 128
let parent x = match x with | [ ] -> assert false | [ _ ] -> None | l -> Some ( list_init l )
let make l = if l = [ ] then raise EmptyModuleHierarchy else l
let lvl x = List . length x - 1
let to_string x = String . concat " . " ( List . map Modname . to_string x )
let of_string x = let l = string_split ' . ' x in make ( List . map Modname . of_string l )
let ml_to_ext path ext = let f = path_basename path in let d = path_dirname path in d </> ( ( chop_extension f ) . <> ( Filetype . to_string ext ) )
let of_modname x = [ x ]
let to_node x = x
let to_dirpath x = if List . length x > 1 then fp ( String . concat Filename . dir_sep ( List . map Modname . to_dir $ list_init x ) ) else currentDir
let append x m = x @ [ m ]
let add_prefix prefix_path hier = if List . length hier <= 1 then prefix_path else begin let to_fp = fp ( String . concat Filename . dir_sep ( List . map Modname . to_dir $ list_init hier ) ) in if ( path_length prefix_path ) = 0 then to_fp else let rec loop path hier_list = match hier_lis...
let check_file path filename ext = if ext <> Filetype . FileOther " " then Ext . Filesystem . exists ( path </> ( ( fn filename ) . <> ( Filetype . to_string ext ) ) ) else Ext . Filesystem . exists ( path </> ( fn filename ) )
let check_modname path modname ext = if ( check_file path modname ext ) then Some modname else let name = String . uncapitalize modname in if ( check_file path name ext ) then Some name else None
let get_filepath root_path hier ext : file_entry option = if ( Hashtbl . mem hiers hier ) then Some ( Hashtbl . find hiers hier ) else let path = add_prefix root_path hier in let modname = Modname . to_string ( leaf hier ) in let res = check_modname path modname ext in match res with | None -> ...
let to_filename hier prefix_path = get_filepath prefix_path hier Filetype . FileML
let to_directory hier prefix_path = get_filepath prefix_path hier ( Filetype . FileOther " " )
let to_generators hier prefix_path = if ( Hashtbl . mem hiers hier ) then Some ( Hashtbl . find hiers hier ) else try Some ( list_findmap ( fun gen -> let path = add_prefix prefix_path hier in let modname = Modname . to_string ( leaf hier ) in let modname = gen . Generators . modname modn...
let get_src_file dst_dir = function | FileEntry ( _ , f ) -> f | GeneratedFileEntry ( _ , _ , fn ) -> dst_dir </> fn | DirectoryEntry ( _ , f ) -> f
let get_dest_file dst_dir ext hier = let entry = Hashtbl . find hiers hier in match entry with | FileEntry ( _ , f ) -> let filename = path_basename f in let path = add_prefix dst_dir hier in path </> ( ( chop_extension filename ) . <> Filetype . to_string ext ) | GeneratedFileEntry ( _ ...
let get_dest_file_ext dst_dir hier ext_f = let entry = Hashtbl . find hiers hier in match entry with | FileEntry ( _ , f ) -> let filename = path_basename f in let filetype = Filetype . of_filepath f in let path = add_prefix dst_dir hier in path </> ( ( chop_extension filename ) . <> Filetyp...
let to_interface hier prefix_path = get_filepath prefix_path hier Filetype . FileMLI
let get_file_entry_maybe hier = if ( Hashtbl . mem hiers hier ) then Some ( Hashtbl . find hiers hier ) else None
let get_file_entry hier paths = if ( Hashtbl . mem hiers hier ) then Hashtbl . find hiers hier else list_findmap ( fun path -> try Some ( list_findmap ( fun lookup -> lookup hier path ) [ to_filename ; to_directory ; to_generators ; to_interface ] ) with Not_found -> None ) paths
let of_filename filename = let name = Filename . chop_extension ( fn_to_string filename ) in let m = try Modname . wrap ( String . capitalize name ) with Modname . EmptyModuleName -> raise ( Modname . ModuleFilenameNotValid ( fn_to_string filename ) ) | Invalid_argument _ -> raise ( M...
let validate_circuit_against_interface ( type i ) ( module I : Interface . S_Of_signal with type Of_signal . t = i ) circuit = let circuit_inputs = Circuit . inputs circuit |> List . map ~ f ( : fun s -> Signal . names s |> List . hd_exn ) |> Set . of_list ( module String ) in le...
let hierarchy ( type i o ) ( module I : Interface . S_Of_signal with type Of_signal . t = i ) ( module O : Interface . S_Of_signal with type Of_signal . t = o ) ? attributes ? config ? instance db ~ name create_fn inputs = let create_inst = Instantiation . create_with_interface ( modul...
let create ~ scope ~ name create_fn inputs = let scope = Scope . sub_scope scope name in create_fn scope inputs ; ;
let hierarchical ( type i o ) ( module I : Interface . S_Of_signal with type Of_signal . t = i ) ( module O : Interface . S_Of_signal with type Of_signal . t = o ) ? config ? instance ? attributes ( ~ scope : Scope . t ) ~ name create_fn inputs = let hierarchy = hierarchy ? attrib...
module With_interface ( I : Interface . S ) ( O : Interface . S ) = struct let create = hierarchy ( module I ) ( module O ) end
module In_scope ( I : Interface . S ) ( O : Interface . S ) = struct type create = Scope . t -> Signal . t Interface . Create_fn ( I ) ( O ) . t let create ~ scope ~ name create_fn inputs = let scope = Scope . sub_scope scope name in let label_ports = Scope . auto_label_hierarc...
module Heavy_hitters ( A : Hashtbl . HashedType ) : sig type t val make : int -> t val add : t -> A . t -> unit val length : t -> int val iter : t -> ( A . t -> int -> int -> unit ) -> unit module Tbl = Hashtbl . Make ( A ) type counter = { mutable hits : int ; mutable misses ...
module Loc_tbl = Hashtbl . Make ( struct type t = Location_code . t let hash ( x : Location_code . t ) = ( ( x :> int ) * 218854569 ) lsr 17 let equal ( x : Location_code . t ) ( y : Location_code . t ) = ( x = y ) end )
module Str_tbl = Hashtbl . Make ( struct type t = string let hash = Hashtbl . hash let equal = String . equal end )
type loc_entry = { line : int ; start_ch : int ; end_ch : int ; func : func ; mutable alloc_count : int ; } id : int ; name : string ; filename : string ; mutable locs : loc_entry list ; mutable total_count : int ; mutable n_allocs : int ; mutable total_dist_to_alloc : int ; }
module Func_tbl = Hashtbl . Make ( struct type t = func let hash ( f : func ) = f . id * 21089245 let equal ( f : func ) ( g : func ) = f . id = g . id end )
let total_allocs ( f : func ) = List . fold_left ( fun k e -> k + e . alloc_count ) f . total_count f . locs
let direct_allocs ( f : func ) = List . fold_left ( fun k e -> k + e . alloc_count ) 0 f . locs
let avg_dist_to_alloc ( f : func ) = float_of_int f . total_dist_to_alloc . / float_of_int f . n_allocs
type loc_table = { entries : loc_entry Loc_tbl . t ; funcs : ( string * string , func ) Hashtbl . t ; trace : Reader . t ; mutable next_id : int ; }
let new_loc_table trace = { entries = Loc_tbl . create 10000 ; funcs = Hashtbl . create 10000 ; trace ; next_id = 0 }
let rec describe_location ( ? max_discard = 2 ) trace buf i = match Reader . lookup_location_code trace buf . ( i ) with | [ ] when i > 0 && max_discard > 0 -> describe_location ~ max_discard ( : max_discard - 1 ) trace buf ( i - 1 ) | [ ] -> " " , ?? Printf . spr...
let add_loc t buf i = let loc = buf . ( i ) in match Loc_tbl . find t . entries loc with | e -> e | exception Not_found -> let filename , funcname , line , start_ch , end_ch = describe_location t . trace buf i in let func = match Hashtbl . find t . funcs ( filename , funcname ) wit...
module HH = Heavy_hitters ( struct type t = func * func let hash ( ( a , b ) : t ) = a . id * 1231441 + b . id * 3821 let equal ( ( a , b ) : t ) ( ( a ' , b ' ) : t ) = ( a . id = a ' . id ) && ( b . id = b ' . id ) end )
let count filename = let trace = Reader . open_ ~ filename in let hh = HH . make 10000 in let seen = Func_tbl . create 100 in let locs = new_loc_table trace in let total_samples = ref 0 in Reader . iter trace ( fun _time ev -> match ev with | Alloc { obj_id = _ ; length = _ ; nsamples ;...
let ( ) = if Array . length Sys . argv <> 2 then Printf . fprintf stderr " Usage : % s < trace file >\ n " Sys . executable_name else count Sys . argv . ( 1 )
type api_gateway_request_identity = { cognito_identity_pool_id : string option [ @ key " cognitoIdentityPoolId " ] ; account_id : string option [ @ key " accountId " ] ; cognito_identity_id : string option [ @ key " cognitoIdentityId " ] ; caller : string option ; access_key :...
type api_gateway_proxy_request_context = { account_id : string [ @ key " accountId " ] ; resource_id : string [ @ key " resourceId " ] ; stage : string ; request_id : string [ @ key " requestId " ] ; identity : api_gateway_request_identity ; resource_path : string [ @ ke...
type api_gateway_proxy_request = { resource : string ; path : string ; http_method : string [ @ key " httpMethod " ] ; headers : string StringMap . t ; query_string_parameters : string StringMap . t [ @ key " queryStringParameters " ] [ @ default StringMap . empty ] ; path...
type api_gateway_proxy_response = { status_code : int [ @ key " statusCode " ] ; headers : string StringMap . t ; body : string ; is_base64_encoded : bool [ @ key " isBase64Encoded " ] }
module API_gateway_request = struct type t = api_gateway_proxy_request [ @@ deriving of_yojson { strict = false } ] end
module API_gateway_response = struct type t = api_gateway_proxy_response [ @@ deriving to_yojson ] let to_yojson t = Lwt . return ( to_yojson t ) end
type penalty = Warn | Fail
type law = { law_name : string ; law_rules : rule list ; law_penalty : penalty }
let list_collect f l = let rec loop result = function | [ ] -> List . rev result | x :: rest -> match f x with | None -> loop result rest | Some y -> loop ( y :: result ) rest in loop [ ] l
let list_none_for_all f l = let rec loop = function | [ ] -> None | x :: rest -> match f x with | None -> loop rest | y -> y in loop l
let check ? sanitize laws entry = let penalties = ref [ ] in let microbes = ref SS . empty in let ( ) = match sanitize with | Some fn -> if sys_file_exists fn then sys_remove fn | None -> ( ) in let remove path name = if sanitize <> None then microbes := SS . add ( filename_concat path nam...
type t = { stamp : int ; name : string ; mutable flags : int }
let currentstamp = ref 0
let create s = incr currentstamp ; { name = s ; stamp = ! currentstamp ; flags = 0 }
let create_predef_exn s = incr currentstamp ; { name = s ; stamp = ! currentstamp ; flags = predef_exn_flag }
let create_persistent s = { name = s ; stamp = 0 ; flags = global_flag }
let rename i = incr currentstamp ; { i with stamp = ! currentstamp }
let name i = i . name
let stamp i = i . stamp
let unique_name i = i . name ^ " _ " ^ string_of_int i . stamp
let unique_toplevel_name i = i . name ^ " " / ^ string_of_int i . stamp
let persistent i = ( i . stamp = 0 )
let equal i1 i2 = i1 . name = i2 . name
let same i1 i2 = i1 = i2
let binding_time i = i . stamp
let set_current_time t = currentstamp := max ! currentstamp t
let reinit_level = ref ( - 1 )
let reinit ( ) = if ! reinit_level < 0 then reinit_level := ! currentstamp else currentstamp := ! reinit_level
let hide i = { i with stamp = - 1 }
let make_global i = i . flags <- i . flags lor global_flag
let global i = ( i . flags land global_flag ) <> 0
let is_predef_exn i = ( i . flags land predef_exn_flag ) <> 0
let print ppf i = match i . stamp with | 0 -> fprintf ppf " % s " ! i . name | - 1 -> fprintf ppf " % s " # i . name | n -> fprintf ppf " % s /% i % s " i . name n ( if global i then " g " else " " )