text
stringlengths
0
601k
let d2 d1 = critical_section ( fun ( ) -> let r = wait_until ( at 3 ) in assert ( r = Timeout ) ) ; check_after 3 ; notify d1 ; assert ( Atomic . get flag ) ; check_before 4
let ( ) = let d1 = spawn d1 in let d2 = spawn ( fun ( ) -> d2 ( get_id d1 ) ) in join d1 ; join d2 ; print_endline " ok "
type source_provenance = | File of file | Pack of string | Startup | Toplevel
type compiler_pass = | All | Parsing of file | Preprocessing of file | Typing of file | Transl of file | Generate of file | Assemble of source_provenance | Clambda of source_provenance | Cmm of source_provenance | Compile_phrases of source_provenance | Selection of source_provenance | Comballoc of source_provenance | C...
let timings : ( compiler_pass , float * float option ) Hashtbl . t = Hashtbl . create 20
let reset ( ) = Hashtbl . clear timings
let start pass = let time = Sys . time ( ) in Hashtbl . add timings pass ( time , None )
let stop pass = assert ( Hashtbl . mem timings pass ) ; let time = Sys . time ( ) in let ( start , stop ) = Hashtbl . find timings pass in assert ( stop = None ) ; Hashtbl . replace timings pass ( start , Some ( time . - start ) )
let time pass f x = start pass ; let r = f x in stop pass ; r
let restart pass = let previous_duration = match Hashtbl . find timings pass with | exception Not_found -> 0 . | ( _ , Some duration ) -> duration | _ , None -> assert false in let time = Sys . time ( ) in Hashtbl . replace timings pass ( time , Some previous_duration )
let accumulate pass = let time = Sys . time ( ) in match Hashtbl . find timings pass with | exception Not_found -> assert false | _ , None -> assert false | ( start , Some duration ) -> let duration = duration . + ( time . - start ) in Hashtbl . replace timings pass ( start , Some duration )
let accumulate_time pass f x = restart pass ; let r = f x in accumulate pass ; r
let get pass = match Hashtbl . find timings pass with | _start , Some duration -> Some duration | _ , None -> None | exception Not_found -> None
let kind_name = function | File f -> Printf . sprintf " sourcefile ( % s ) " f | Pack p -> Printf . sprintf " pack ( % s ) " p | Startup -> " startup " | Toplevel -> " toplevel "
let pass_name = function | All -> " all " | Parsing file -> Printf . sprintf " parsing ( % s ) " file | Preprocessing file -> Printf . sprintf " preprocessing ( % s ) " file | Typing file -> Printf . sprintf " typing ( % s ) " file | Transl file -> Printf . sprintf " transl ( % s ) " file | Generate file -> Printf . sp...
let timings_list ( ) = let l = Hashtbl . fold ( fun pass times l -> ( pass , times ) :: l ) timings [ ] in List . sort ( fun ( _ , ( start1 , _ ) ) ( _ , ( start2 , _ ) ) -> compare start1 start2 ) l
let print ppf = let current_time = Sys . time ( ) in List . iter ( fun ( pass , ( start , stop ) ) -> match stop with | Some duration -> Format . fprintf ppf " % s : . % 03fs . " @ ( pass_name pass ) duration | None -> Format . fprintf ppf " % s : running since . % 03fs . " @ ( pass_name pass ) ( current_time . - start...
let sexp_of_t_style : [ ` Pretty | ` Internal ] ref = ref ` Pretty
module Num_key_bits : sig type t = private int [ @@ deriving compare , sexp ] include Comparable with type t := t include Invariant . S with type t := t val zero : t val max_value : t val to_int : t -> int val of_int : int -> t val ( + ) : t -> t -> t val ( - ) : t -> t -> t val pow2 : t -> Int63 . t include Int let mi...
module Level_bits = struct type t = Num_key_bits . t list [ @@ deriving compare , sexp ] let max_num_bits = ( Num_key_bits . max_value :> int ) let num_bits_internal t = List . fold t ~ init : Num_key_bits . zero ~ f : Num_key_bits . ( + ) let num_bits t = ( num_bits_internal t :> int ) let invariant t = assert ( not (...
module Alarm_precision : sig include Alarm_precision val num_key_bits : t -> Num_key_bits . t val interval_num : t -> Time_ns . t -> Int63 . t val interval_num_start : t -> Int63 . t -> Time_ns . t type t = int [ @@ deriving compare , hash ] let equal = [ % compare . equal : t ] let num_key_bits t = t |> Num_key_bits ....
module Config = struct let level_bits_default = Level_bits . default type t = { alarm_precision : Alarm_precision . Unstable . t ; level_bits : Level_bits . t [ @ default level_bits_default ] ; capacity : int option [ @ sexp . option ] } [ @@ deriving fields , sexp ] let alarm_precision t = Alarm_precision . to_span t ...
module Priority_queue : sig type ' a t [ @@ deriving sexp_of ] type ' a priority_queue = ' a t module Key : Interval_num module Elt : sig type ' a t [ @@ deriving sexp_of ] val at : ' a priority_queue -> ' a t -> Time_ns . t val key : ' a priority_queue -> ' a t -> Key . t val value : ' a priority_queue -> ' a t -> ' a...
type ' a t = { config : Config . t ; start : Time_ns . t ; max_interval_num : Interval_num . t ; mutable now : Time_ns . t ; mutable now_interval_num_start : Time_ns . t ; mutable max_allowed_alarm_time : Time_ns . t ; priority_queue : ' a Priority_queue . t }
type ' a timing_wheel = ' a t
type ' a t_now = ' a t
let sexp_of_t_now _ t = [ % sexp ( t . now : Time_ns . t ) ]
let alarm_precision t = Config . alarm_precision t . config
module Alarm = struct type ' a t = ' a Priority_queue . Elt . t [ @@ deriving sexp_of ] let null = Priority_queue . Elt . null let at tw t = Priority_queue . Elt . at tw . priority_queue t let value tw t = Priority_queue . Elt . value tw . priority_queue t let interval_num tw t = Priority_queue . Elt . key tw . priorit...
let iter t ~ f = Priority_queue . iter t . priority_queue ~ f
module Pretty = struct module Alarm = struct type ' a t = { at : Time_ns . t ; value : ' a } [ @@ deriving fields , sexp_of ] let create t alarm = { at = Alarm . at t alarm ; value = Alarm . value t alarm } let compare t1 t2 = Time_ns . compare ( at t1 ) ( at t2 ) end type ' a t = { config : Config . t ; start : Time_n...
let pretty ( { config ; start ; max_interval_num ; now ; now_interval_num_start = _ ; max_allowed_alarm_time = _ ; priority_queue = _ } as t ) = let r = ref [ ] in iter t ~ f ( : fun a -> r := Pretty . Alarm . create t a :: ! r ) ; let alarms = List . sort ! r ~ compare : Pretty . Alarm . compare in { Pretty . config ;...
let sexp_of_t sexp_of_a t = match ! sexp_of_t_style with | ` Internal -> sexp_of_t_internal sexp_of_a t | ` Pretty -> [ % sexp ( pretty t : a Pretty . t ) ] ; ;
let length t = Priority_queue . length t . priority_queue
let is_empty t = length t = 0
let interval_num_internal ~ time ~ alarm_precision = Interval_num . of_int63 ( Alarm_precision . interval_num alarm_precision time ) ; ;
let interval_num_unchecked t time = interval_num_internal ~ time ~ alarm_precision : t . config . alarm_precision ; ;
let interval_num t time = if Time_ns . ( < ) time min_time then raise_s [ % message " Timing_wheel . interval_num got time too far in the past " ( time : Time_ns . t ) ] ; interval_num_unchecked t time ; ;
let interval_num_start_unchecked t interval_num = Alarm_precision . interval_num_start t . config . alarm_precision ( interval_num |> Interval_num . to_int63 ) ; ; raise_s [ % message " Timing_wheel . interval_num_start got too small interval_num " ( interval_num : Interval_num . t ) ( min_interval_num : Interval_num ....
let interval_num_start t interval_num = if Interval_num . ( < ) interval_num min_interval_num then raise_interval_num_start_got_too_small interval_num ; if Interval_num . ( > ) interval_num t . max_interval_num then raise_interval_num_start_got_too_large t interval_num ; interval_num_start_unchecked t interval_num ; ;
let compute_max_allowed_alarm_time t = let max_allowed_key = Priority_queue . max_allowed_key t . priority_queue in if Interval_num . ( >= ) max_allowed_key t . max_interval_num then max_time else Time_ns . add ( interval_num_start_unchecked t max_allowed_key ) ( Time_ns . Span . ( - ) ( alarm_precision t ) Time_ns . S...
let now_interval_num t = Priority_queue . min_allowed_key t . priority_queue
let max_allowed_alarm_interval_num t = interval_num t ( max_allowed_alarm_time t )
let interval_start t time = interval_num_start_unchecked t ( interval_num t time )
let invariant invariant_a t = Invariant . invariant [ % here ] t [ % sexp_of : _ t ] ( fun ( ) -> let check f = Invariant . check_field t f in Fields . iter ~ config ( : check Config . invariant ) ~ start : ( check ( fun start -> assert ( Time_ns . ( >= ) start min_time ) ; assert ( Time_ns . ( <= ) start max_time ) ) ...
let advance_clock t ~ to_ ~ handle_fired = if Time_ns . ( > ) to_ ( now t ) then ( t . now <- to_ ; let key = interval_num_unchecked t to_ in t . now_interval_num_start <- interval_num_start_unchecked t key ; Priority_queue . increase_min_allowed_key t . priority_queue ~ key ~ handle_removed : handle_fired ; t . max_al...
let create ~ config ~ start = if Time_ns . ( < ) start Time_ns . epoch then raise_s [ % message " Timing_wheel . create got start before the epoch " ( start : Time_ns . t ) ] ; let t = { config ; start ; max_interval_num = interval_num_internal ~ time : max_time ~ alarm_precision : config . alarm_precision ; now = Time...
let add_at_interval_num t ~ at value = Internal_elt . to_external ( Priority_queue . internal_add t . priority_queue ~ key : at ~ at ( : interval_num_start t at ) value ) ; ; raise_s [ % message " Timing_wheel cannot schedule alarm that far in the future " ( at : Time_ns . t ) ~ max_allowed_alarm_time ( : t . max_allow...
let ensure_can_schedule_alarm t ~ at = if Time_ns . ( > ) at t . max_allowed_alarm_time then raise_that_far_in_the_future t at ; if Time_ns . ( < ) at t . now_interval_num_start then raise_before_start_of_current_interval t at ; ;
let add t ~ at value = ensure_can_schedule_alarm t ~ at ; Internal_elt . to_external ( Priority_queue . internal_add t . priority_queue ~ key ( : interval_num_unchecked t at ) ~ at value ) ; ;
let remove t alarm = Priority_queue . remove t . priority_queue alarm
let clear t = Priority_queue . clear t . priority_queue
let mem t alarm = Priority_queue . mem t . priority_queue alarm
let reschedule_gen t alarm ~ key ~ at = if not ( mem t alarm ) then failwith " Timing_wheel cannot reschedule alarm not in timing wheel " ; ensure_can_schedule_alarm t ~ at ; Priority_queue . change t . priority_queue alarm ~ key ~ at ; ;
let reschedule t alarm ~ at = reschedule_gen t alarm ~ key ( : interval_num_unchecked t at ) ~ at ; ;
let reschedule_at_interval_num t alarm ~ at = reschedule_gen t alarm ~ key : at ~ at ( : interval_num_start t at ) ; ;
let pool t = Priority_queue . pool t . priority_queue
let min_alarm_interval_num t = let elt = Priority_queue . min_elt_ t . priority_queue in if Internal_elt . is_null elt then None else Some ( Internal_elt . key ( pool t ) elt ) ; ;
let min_alarm_interval_num_exn t = let elt = Priority_queue . min_elt_ t . priority_queue in if Internal_elt . is_null elt then raise_s [ % message " Timing_wheel . min_alarm_interval_num_exn of empty timing_wheel " ~ timing_wheel ( : t : _ t ) ] else Internal_elt . key ( pool t ) elt ; ;
let max_alarm_time_in_list t elt = let pool = pool t in Internal_elt . max_alarm_time pool elt ~ with_key ( : Internal_elt . key pool elt ) ; ;
let max_alarm_time_in_min_interval t = let elt = Priority_queue . min_elt_ t . priority_queue in if Internal_elt . is_null elt then None else Some ( max_alarm_time_in_list t elt ) ; ;
let max_alarm_time_in_min_interval_exn t = let elt = Priority_queue . min_elt_ t . priority_queue in if Internal_elt . is_null elt then raise_s [ % message " Timing_wheel . max_alarm_time_in_min_interval_exn of empty timing wheel " ~ timing_wheel ( : t : _ t ) ] ; max_alarm_time_in_list t elt ; ;
let next_alarm_fires_at_internal t key = interval_num_start t ( Key . succ key ) ; ;
let next_alarm_fires_at t = let elt = Priority_queue . min_elt_ t . priority_queue in if Internal_elt . is_null elt then None else ( let key = Internal_elt . key ( pool t ) elt in if Interval_num . equal key t . max_interval_num then None else Some ( next_alarm_fires_at_internal t key ) ) ; ; raise_s [ % message " Timi...
let next_alarm_fires_at_exn t = let elt = Priority_queue . min_elt_ t . priority_queue in if Internal_elt . is_null elt then raise_next_alarm_fires_at_exn_of_empty_timing_wheel t ; let key = Internal_elt . key ( pool t ) elt in if Interval_num . equal key t . max_interval_num then raise_next_alarm_fires_at_with_all_ala...
let fire_past_alarms t ~ handle_fired = Priority_queue . fire_past_alarms t . priority_queue ~ handle_fired ~ key ( : now_interval_num t ) ~ now : t . now ; ;
module Private = struct module Num_key_bits = Num_key_bits let interval_num_internal = interval_num_internal let max_time = max_time end
module type Interval_num = sig module Span : sig type t = private Int63 . t [ @@ deriving sexp_of ] include Comparable . S with type t := t val max : t -> t -> t val zero : t val one : t val max_value : t val of_int63 : Int63 . t -> t val to_int63 : t -> Int63 . t val of_int : int -> t val to_int_exn : t -> int val sca...
module type Alarm_precision = sig type t [ @@ deriving compare , sexp_of ] include Equal . S with type t := t val of_span : Time_ns . Span . t -> t [ @@ deprecated " [ since 2018 - 01 ] Use [ of_span_floor_pow2_ns ] " ] val of_span_floor_pow2_ns : Time_ns . Span . t -> t val to_span : t -> Time_ns . Span . t val one_na...
module type Timing_wheel = sig module Alarm_precision : Alarm_precision type ' a t [ @@ deriving sexp_of ] type ' a timing_wheel = ' a t type ' a t_now = ' a t [ @@ deriving sexp_of ] module Interval_num : Interval_num module Alarm : sig type ' a t [ @@ deriving sexp_of ] val null : unit -> _ t val at : ' a timing_whee...
type ' a run = Asc of ' a list | Desc of ' a list | [ ] | [ _ ] -> true | x :: y :: r -> compare x y <= 0 && sorted_list compare ( y :: r )
type ' a cmp = ' a -> ' a -> int match l with [ ] -> ( ) | [ _ ] -> ( ) | y1 :: ys -> sorted_mem cmp y1 ys | [ ] | [ _ ] -> true | x :: y :: r -> compare x y >= 0 && sorted_list_rev compare ( y :: r ) match l with [ ] -> ( ) | [ _ ] -> ( ) | y1 :: ys -> sorted_rev_mem cmp y1 ys
let rec next_asc_run_rev compare revrun last len = function | x :: xs -> if compare last x <= 0 then next_asc_run_rev compare ( last :: revrun ) x ( len + 1 ) xs else let xs = x :: xs in ( len , Desc ( last :: revrun ) , xs ) | xs -> ( len , Desc ( last :: revrun ) , xs )
let rec next_desc_run_rev compare ( revrun : ' a list ) last len = function | x :: xs -> if compare last x > 0 then next_desc_run_rev compare ( last :: revrun ) x ( len + 1 ) xs else let xs = x :: xs in ( len , Asc ( last :: revrun ) , xs ) | xs -> ( len , Asc ( last :: revrun ) , xs )
let next_run compare xs = match xs with | [ ] -> None | [ _ ] -> Some ( 1 , Asc xs , [ ] ) | x1 :: x2 :: xs -> if compare x1 x2 <= 0 then Some ( next_asc_run_rev compare [ x1 ] x2 2 xs ) else Some ( next_desc_run_rev compare [ x1 ] x2 2 xs )
let merge_asc_rev compare xs ys = let rec merge_asc_rev revacc xs ys = match ( xs , ys ) with | [ ] , zs | zs , [ ] -> List . rev_append zs revacc | x :: xs ' , y :: ys ' -> if compare x y <= 0 then merge_asc_rev ( x :: revacc ) xs ' ys else merge_asc_rev ( y :: revacc ) xs ys ' in merge_asc_rev [ ] xs ys
let merge_desc_rev compare = let rec merge_desc_rev revacc xs ys = match ( xs , ys ) with | [ ] , zs | zs , [ ] -> List . rev_append zs revacc | x :: xs ' , y :: ys ' -> if compare x y > 0 then merge_desc_rev ( x :: revacc ) xs ' ys else merge_desc_rev ( y :: revacc ) xs ys ' in merge_desc_rev [ ]
let merge compare n1 n2 r1 r2 = let run = match ( r1 , r2 ) with | Asc r1 , Asc r2 -> Desc ( merge_asc_rev compare r1 r2 ) | Desc r1 , Desc r2 -> Asc ( merge_desc_rev compare r1 r2 ) | Asc r1 , Desc r2 -> if n1 < n2 then Asc ( merge_desc_rev compare ( List . rev r1 ) r2 ) else Desc ( merge_asc_rev compare r1 ( List . r...
type ' a stack = ( int * ' a run ) list
let timsort compare l = let merge = merge compare in let next_run = next_run compare in let rec fetch_next_run remaining stack = match next_run remaining with | Some ( n , r , rem ) -> sort rem ( ( n , r ) :: stack ) | None -> stack and sort remaining stack = match stack with | ( ( n1 , r1 ) as s1 ) :: ( n2 , r2 ) :: (...
type untyped_ocaml_value = UInt of int
let rec string_of_untyped = function UInt i -> Printf . sprintf " UInt % i " i List . fold_left ( ^ ) " " ( List . map ( fun x -> Printf . sprintf " % s ; " ( string_of_untyped x ) ) ( Array . to_list arr ) ) List . fold_left ( ^ ) " " ( List . map ( fun x -> Printf . sprintf " % f ; " x ) ( Array . to_list arr ) )
let rec find_tag_in_constdecls x isblock tblock tnonblock = function { pcd_name = { txt } ; pcd_args = Pcstr_tuple [ ] } :: _ when x = tnonblock && not isblock -> ( txt , [ ] )
let find_tag_in_variant_type x isblock = function | Ptype_variant constdecls -> Some ( find_tag_in_constdecls x isblock 0 0 constdecls ) | Ptype_abstract -> None | Ptype_record _ -> None | Ptype_open -> None
let rec lookup_variant_type ( env : Tinyocaml . env ) vartypename x isblock = match env with EnvType ( _ , [ { ptype_name = { txt } ; ptype_kind } ] ) :: r when txt = vartypename -> begin match find_tag_in_variant_type x isblock ptype_kind with Some x -> x | None -> lookup_variant_type r vartypename x isblock end | _ :...
let rec read_untyped env debug_typ v typ = match v , typ . ptyp_desc with | UInt n , Ptyp_constr ( { txt = Longident . Lident " int " } , _ ) -> Int n | UInt n , Ptyp_constr ( { txt = Longident . Lident " bool " } , _ ) -> Bool ( n <> 0 ) | UInt n , Ptyp_constr ( { txt = Longident . Lident " char " } , _ ) -> Char ( ch...
let parse_type typ = typ |> Lexing . from_string |> Parse . core_type
let of_ocaml_value env x typ = read_untyped env typ ( untyped_of_ocaml_value x ) ( parse_type typ )
type op = Add | Sub | Mul | Div
type cmp = LT | EQ | GT | EQLT | EQGT | NEQ
type control = Underline | Bold
type forkind = UpTo | DownTo
type pattern = PatAny EnvBinding of bool * binding list ref ModTypeSignature of t TypChar Unit
let rec iter f x = f x ; match x with | ( Bool _ | Float _ | Var _ | Int _ | Int32 _ | Int64 _ | NativeInt _ | Char _ | String _ | OutChannel _ | InChannel _ | Unit | Nil | ModuleIdentifier _ | Raise ( _ , None ) | ExceptionDef _ | TypeDef _ | Open _ ) -> ( ) | Control ( _ , a ) | Field ( a , _ ) | Raise ( _ , Some a )...
let rec recurse f exp = match exp with | ( Bool _ | Float _ | Var _ | Int _ | Int32 _ | Int64 _ | NativeInt _ | Char _ | String _ | OutChannel _ | InChannel _ | Unit | Nil | ModuleIdentifier _ ) as x -> x | Op ( op , a , b ) -> Op ( op , f a , f b ) | And ( a , b ) -> And ( f a , f b ) | Or ( a , b ) -> Or ( f a , f b ...
let string_of_op = function Add -> " " + | Sub -> " " - | Mul -> " " * | Div -> " " /
let string_of_cmp = function LT -> " " < | EQ -> " " = | GT -> " " > | EQLT -> " " <= | EQGT -> " " >= | NEQ -> " " <>
let op_of_string = function " " + -> Add | " " - -> Sub | " " * -> Mul | " " / -> Div
let cmp_of_string = function " " < -> LT | " " = -> EQ | " " > -> GT | " " <= -> EQLT | " " >= -> EQGT | " " <> -> NEQ
let string_of_coretype t = let f = Format . str_formatter in Pprintast . core_type f t ; Format . flush_str_formatter ( )