text
stringlengths
12
786k
let derive_glbfun_table globals_table = let glbfun_table = Hashtbl . create 7 in let rec recurse initvalue = match initvalue with | Unknown -> ( ) | Block b -> Array . iter recurse b | FuncInEnv { env ; _ } -> Array . iteri ( fun func_offset env_val -> match env_val with | Function { la...
let trace_stack_max depth instr = match instr with | I . Kapply num -> if num < 4 then depth + 3 else depth | Kappterm ( num , slots ) -> max depth ( depth - slots + num ) | Kpushtrap _ -> depth + 4 | _ -> depth
let trace_stack_instr depth instr = match instr with | I . Klabel _ -> assert false | Krestart -> depth | Kconst _ | Kacc _ | Kassign _ | Kenvacc _ | Kgetglobal _ -> depth | Kpush -> depth + 1 | Kpop num -> depth - num | ( Knegint | Kboolnot | Koffsetint _ | Koffsetref _ | Kisint | K...
let max_stack_depth_of_instrs instrs = let len = Array . length instrs in let rec recurse mdepth depth k = if k < len then let mdep = trace_stack_max depth instrs . ( k ) in let depth ' = trace_stack_instr depth instrs . ( k ) in recurse ( max mdepth ( max mdep depth ' ) ) depth ' ( ...
let local_branch_labels = function | I . Kbranch l -> [ l ] | Kbranchif l -> [ l ] | Kbranchifnot l -> [ l ] | Kswitch ( la1 , la2 ) -> Array . to_list la1 @ Array . to_list la2 | _ -> [ ]
let rec dump block indent = let open Wc_control in let open Wc_util in let istr = String . make ( 4 * indent ) ' ' in eprintf " % sBLOCK % s % s \ n " istr ( match block . loop_label with | Some l -> sprintf " loop =% d " l | None -> " " ) ( match block . break_label with |...
let max_stack_depth_of_fblock fblock = let open Wc_control in let depth_table = Hashtbl . create 7 in let update_depth_table depth labels = List . iter ( fun label -> try let d = Hashtbl . find depth_table label in if d <> depth then ( eprintf " [ DEBUG ] Bad function : % d \ n " fblock . ...
type state = { camlstack : store list ; camldepth : int ; accu : store ; realaccu : ISet . t ; arity : int ; }
type lpad = { locals : ( string , repr ) Hashtbl . t ; mutable avoid_locals : bool ; mutable loops : ISet . t ; indegree : ( int , int ) Hashtbl . t ; state_table : ( int , state ) Hashtbl . t ; mutable globals_table : ( int , Wc_traceglobals . initvalue ) Hashtbl . ...
let real_accu = RealAccu { no_function = false }
let real_accu_no_func = RealAccu { no_function = true }
let empty_state = { camlstack = [ ] ; camldepth = 0 ; accu = Invalid ; realaccu = ISet . empty ; arity = 1 ; }
let empty_lpad ( ) = { locals = Hashtbl . create 7 ; avoid_locals = false ; loops = ISet . empty ; indegree = Hashtbl . create 7 ; state_table = Hashtbl . create 7 ; globals_table = Hashtbl . create 1 ; environment = [ | ] ; | func_offset = 0 ; }
let new_local lpad repr = let k = Hashtbl . length lpad . locals in let s = sprintf " x % d " k in Hashtbl . add lpad . locals s repr ; s
let stack_descr state = let cd = state . camldepth in let rd = ref 0 in let stack = Array . make cd false in List . iter ( fun st -> match st with | RealStack pos -> assert ( pos >= ( - cd ) ) ; if pos < 0 then ( stack . ( pos + cd ) <- true ; rd := max ! rd ( - pos ) ) ...
let set_camlstack pos store state = let cd = state . camldepth in if pos >= ( - cd ) && pos <= ( - 1 ) then let camlstack = List . mapi ( fun i old_store -> if ( - cd + i ) = pos then store else old_store ) state . camlstack in camlstack else state . camlstack
let pop_camlstack state = let cd = state . camldepth in let cpos = ( - cd ) in match state . camlstack with | RealStack pos :: tl -> { state with camlstack = tl ; camldepth = cd - 1 ; } | ( RealAccu _ ) :: tl -> { state with camlstack = tl ; camldepth = cd - 1 ; realaccu = ...
let rec popn_camlstack number state = if number = 0 then state else popn_camlstack ( number - 1 ) ( pop_camlstack state )
let push_camlstack store state = let cd = state . camldepth in let cpos = ( - cd - 1 ) in match store with | RealStack pos -> { state with camlstack = store :: state . camlstack ; camldepth = cd + 1 ; } | RealAccu _ -> { state with camlstack = store :: state . camlstack ; camldep...
let flush_accu lpad state = let instrs_rev = ISet . fold ( fun pos instr_acc -> let instr = Wcopy { src = real_accu ; dest = RealStack pos } in instr :: instr_acc ) state . realaccu [ ] in let instrs = List . rev instrs_rev in let camlstack = List . mapi ( fun i old -> let pos = ( ...
let straighten_accu lpad state = let state , instrs_flush = flush_accu lpad state in match state . accu with | Invalid -> ( state , [ ] ) | RealAccu _ -> ( state , instrs_flush ) | store -> let instr = Wcopy { src = store ; dest = real_accu } in let state = { state with accu = ...
let straighten_accu_when_on_stack lpad state = match state . accu with | ( RealStack _ ) -> straighten_accu lpad state | _ -> ( state , [ ] )
let straighten_accu_for_branch lpad state = match state . accu with | RealStack _ | Const _ | Atom _ | TracedGlobal _ -> straighten_accu lpad state | Local _ | RealAccu _ | Invalid -> ( state , [ ] )
let pop_real_stack lpad state num = let cd = state . camldepth in match state . accu with | ( RealStack pos ) when pos >= ( - cd ) && pos <= ( - cd + num - 1 ) -> straighten_accu lpad state | _ -> ( state , [ ] )
let flush_real_stack_only_accu_at lpad state pos = match state . accu with | ( RealStack p ) when p = pos -> straighten_accu lpad state | _ -> ( state , [ ] )
let patch camlstack depth patches = let rec recurse camlstack pos patches = match patches with | [ ] -> camlstack | next_patch :: patches ' -> if pos = next_patch then match camlstack with | _ :: tl -> RealStack pos :: ( recurse tl ( pos + 1 ) patches ' ) | [ ] -> assert false else ...
let flush_real_stack_at lpad state pos = let cd = state . camldepth in let state , instrs1 = flush_real_stack_only_accu_at lpad state pos in let _ , positions = List . fold_left ( fun ( q , acc ) store -> match store with | RealStack p when p = pos && q <> p -> assert ( p > q ) ; let a...
let straighten_stack_at lpad state pos = if pos >= 0 then ( state , [ Wcomment ( sprintf " ****** STRANGE CASE : straighten_stack_at pos =% d " ***** pos ) ] ) else let k = pos + state . camldepth in let store = List . nth state . camlstack k in match store with | RealAccu _ -> asse...
let straighten_stack_multi lpad state pos_list = let state , rev_acc = List . fold_left ( fun ( state , rev_acc ) pos -> let ( state , instrs ) = straighten_stack_at lpad state pos in ( state , List . rev_append instrs rev_acc ) ) ( state , [ ] ) pos_list in ( state , List ...
let accu_is_realaccu state = match state . accu with | RealAccu _ -> true | _ -> false
let accu_is_realaccu_or_invalid state = match state . accu with | RealAccu _ | Invalid -> true | _ -> false
let straighten_stack lpad state = let state , instrs1 = flush_accu lpad state in let cd = state . camldepth in let pos_list = state . camlstack |> List . mapi ( fun i store -> ( i , store ) ) |> List . concat_map ( fun ( i , store ) -> match store with | RealStack p when p <> - cd...
let straighten_all lpad state = let state , instrs1 = straighten_accu lpad state in assert ( accu_is_realaccu_or_invalid state && state . realaccu = ISet . empty ) ; let state , instrs2 = straighten_stack lpad state in ( state , instrs1 @ instrs2 )
let localize_accu lpad state repr = match state . accu with | RealStack _ | RealAccu _ -> let local = new_local lpad repr in let lstore = Local ( repr , local ) in let instrs = [ Wcopy { src = state . accu ; dest = lstore } ] in let cd = state . camldepth in let positions = List . m...
let unary_operation ( ? no_function = false ) lpad state op_repr op_code = let src1 = state . accu in let op_repr = if lpad . avoid_locals then RValue else op_repr in match op_repr with | RValue -> let state , instrs_flush = flush_accu lpad state in let state = { state with accu = real_accu } in...
let unary_effect lpad state op_code = let src1 = state . accu in let state = { state with accu = Const 0 } in let instrs_op = [ Wunaryeffect { op = op_code ; src1 } ] in ( state , instrs_op )
let binary_operation ( ? no_function = false ) lpad state op_repr op_code = let src1 = state . accu in let src2 = List . hd state . camlstack in let op_repr = if lpad . avoid_locals then RValue else op_repr in match op_repr with | RValue -> let state , instrs_flush = flush_accu lpad state in let ...
let binary_effect lpad state op_code = let src1 = state . accu in let src2 = List . hd state . camlstack in let state = { state with accu = Const 0 } |> pop_camlstack in let instrs_op = [ Wbinaryeffect { op = op_code ; src1 ; src2 } ] in ( state , instrs_op )
let ternary_effect lpad state op_code = let src1 = state . accu in let src2 = List . hd state . camlstack in let src3 = List . hd ( List . tl state . camlstack ) in let state = { state with accu = Const 0 } |> pop_camlstack |> pop_camlstack in let instrs_op = [ Wternaryeffect { op = o...
let global_offset ident = assert ( Ident . global ident ) ; let name = Ident . name ident in int_of_string name
let make_label lpad label = if ISet . mem label lpad . loops then Loop label else Label label
let validate state = let d = state . camldepth in List . iteri ( fun i st -> match st with | RealStack pos -> assert ( pos = ( - d + i ) ) | _ -> assert false ) state . camlstack ; assert ( match state . accu with | RealAccu { no_function = true } -> false | RealAccu _ | Loc...
let update_state_table lpad state label = try let s = Hashtbl . find lpad . state_table label in assert ( s . camldepth = state . camldepth ) with | Not_found -> Hashtbl . add lpad . state_table label state
let norm_accu state = match state . accu with | RealAccu { no_function = true } -> { state with accu = real_accu } | _ -> state
let branch lpad state label = let state = norm_accu state in let instr_br = Wbranch { label = make_label lpad label } in try let dest_state = Hashtbl . find lpad . state_table label in validate dest_state ; let ( state , instrs1 ) = straighten_accu_for_branch lpad state in let ( state , instrs...
let transl_instr lpad state instr = match instr with | I . Klabel _ -> assert false | Kconst ( Lambda . Const_base ( Asttypes . Const_int k ) ) -> ( { state with accu = Const k } , [ ] ) | Kconst _ -> assert false | Kacc sp -> let state = if sp < state . camldepth then { st...
let local_branch_labels = function | I . Kbranch l -> [ l ] | Kbranchif l -> [ l ] | Kbranchifnot l -> [ l ] | Kswitch ( la1 , la2 ) -> Array . to_list la1 @ Array . to_list la2 | _ -> [ ]
let transl_fblock lpad fblock = let open Wc_control in let indegree = Hashtbl . create 7 in let state_table = Hashtbl . create 7 in let lpad = { lpad with indegree ; state_table } in let incr_indegree lab = let n = try Hashtbl . find indegree lab with Not_found -> 0 in if n >= 0 then Hashtbl . ...
let string_of_raise_kind = function | Lambda . Raise_regular -> " regular " | Raise_reraise -> " reraise " | Raise_notrace -> " notrace "
let string_of_int_comparison = function | Lambda . Ceq -> " eq " | Cne -> " ne " | Clt -> " lt " | Cgt -> " gt " | Cle -> " le " | Cge -> " ge "
let string_of_kinstruction = function | I . Klabel label -> sprintf " Klabel ( label % d ) " label | Kacc k -> sprintf " Kacc ( % d ) " k | Kenvacc k -> sprintf " Kenvacc ( % d ) " k | Kpush -> " Kpush " | Kpop k -> sprintf " Kpop ( % d ) " k | Kassign k -> sprintf...
let hexdigits = [ | ' 0 ' ; ' 1 ' ; ' 2 ' ; ' 3 ' ; ' 4 ' ; ' 5 ' ; ' 6 ' ; ' 7 ' ; ' 8 ' ; ' 9 ' ; ' a ' ; ' b ' ; ' c ' ; ' d ' ; ' e ' ; ' f ' ] |
let rec enum k n = if n > 0 then k :: enum ( k + 1 ) ( n - 1 ) else [ ]
let rec list_prefix n l = if n = 0 then [ ] else match l with | x :: l ' -> x :: ( list_prefix ( n - 1 ) l ' ) | [ ] -> assert false
let create l = if not ( 0 <= l && l <= Obj . Ephemeron . max_ephe_length ) then invalid_arg ( " Weak . create " ) ; create l
let length x = Obj . size ( Obj . repr x ) - additional_values
let raise_if_invalid_offset e o msg = if not ( 0 <= o && o < length e ) then invalid_arg ( msg )
let set e o x = raise_if_invalid_offset e o " Weak . set " ; match x with | None -> unset e o | Some x -> set ' e o x
let get e o = raise_if_invalid_offset e o " Weak . get " ; get e o
let get_copy e o = raise_if_invalid_offset e o " Weak . get_copy " ; get_copy e o
let check e o = raise_if_invalid_offset e o " Weak . check " ; check e o
let blit e1 o1 e2 o2 l = if l < 0 || o1 < 0 || o1 > length e1 - l || o2 < 0 || o2 > length e2 - l then invalid_arg " Weak . blit " else if l <> 0 then blit e1 o1 e2 o2 l
let fill ar ofs len x = if ofs < 0 || len < 0 || ofs > length ar - len then raise ( Invalid_argument " Weak . fill " ) else begin for i = ofs to ( ofs + len - 1 ) do set ar i x done end
module type S = sig type data type t val create : int -> t val clear : t -> unit val merge : t -> data -> data val add : t -> data -> unit val remove : t -> data -> unit val find : t -> data -> data val find_opt : t -> data -> data option val find_all : t -> data -> data list val mem : t -> dat...
module Make ( H : Hashtbl . HashedType ) : ( S with type data = H . t ) = struct type ' a weak_t = ' a t let weak_create = create let emptybucket = weak_create 0 type data = H . t type t = { mutable table : data weak_t array ; mutable hashes : int array array ; mutable limit : int ;...
type ' a t = { mutable size : int ; mutable array : ' a Weak . t ; }
let create n = { size = 0 ; array = Weak . create ( max 1 n ) }
let clear xs = let threshold = Weak . length xs . array * 2 / 3 in if xs . size < threshold then Obj . truncate ( Obj . repr xs . array ) ( 1 + threshold ) ; xs . size <- 0
let exists xs x = let rec loop i = if i < xs . size then match Weak . get xs . array i with | Some y when x == y -> true | _ -> loop ( i + 1 ) else false in loop 0
let add xs x = if not ( exists xs x ) then ( if Weak . length xs . array = xs . size then begin let array = Weak . create ( xs . size * 3 / 2 + 1 ) in let j = ref 0 in for i = 0 to xs . size - 1 do match Weak . get xs . array i with | Some _ as x ' opt -> Weak . set array...
let fold fn xs acc = let acc = ref acc in let j = ref 0 in for i = 0 to xs . size - 1 do match Weak . get xs . array i with | Some x as x ' opt -> acc := fn x ! acc ; if ! j < i then Weak . set xs . array ! j x ' opt ; incr j | None -> ( ) done ; xs . size <- ! j ; ! acc
type typed_dictionary_mismatch = | MissingRequiredField of { field_name : Identifier . t ; class_name : Identifier . t ; } | FieldTypeMismatch of { field_name : Identifier . t ; expected_type : Type . t ; actual_type : Type . t ; class_name : Identifier . t ; } | UndefinedField...
type weakened_type = { resolved : Type . t ; typed_dictionary_errors : typed_dictionary_mismatch Node . t list ; }
let typed_dictionary_errors { typed_dictionary_errors ; _ } = typed_dictionary_errors
let resolved_type { resolved ; _ } = resolved
let make_weakened_type ( ? typed_dictionary_errors = [ ] ) resolved = { resolved ; typed_dictionary_errors }
let combine_weakened_types weakened_types = { resolved = Type . union ( List . map weakened_types ~ f : resolved_type ) ; typed_dictionary_errors = List . concat_map weakened_types ~ f : typed_dictionary_errors ; }
let undefined_field_mismatches ~ location ~ expected_typed_dictionary : { Type . Record . TypedDictionary . fields = expected_fields ; name = class_name } ~ resolved_typed_dictionary { : Type . Record . TypedDictionary . fields = resolved_fields ; _ } = let make_undefined_field_mismatc...
let distribute_union_over_parametric ~ parametric_name ~ number_of_parameters annotation = match annotation with | Type . Union parameters -> let extract_matching_parameters = function | Type . Parametric { name ; parameters } when Identifier . equal name parametric_name && List . length parameter...
let rec weaken_mutable_literals resolve ~ resolve_items_individually ~ get_typed_dictionary ~ expression ~ resolved ~ expected ~ comparator = let comparator_without_override = comparator in let comparator = comparator ~ get_typed_dictionary_override ( : fun _ -> None ) in let open Expression in match e...
let weaken_mutable_literals = weaken_mutable_literals ~ resolve_items_individually : false
type block = int array ; ;
type objdata = | Present of block | Absent of int ; ;
type bunch = { objs : objdata array ; wp : block Weak . t ; } ; ;
let data = Array . init size ( fun i -> let n = 1 + Random . int size in { objs = Array . make n ( Absent 0 ) ; wp = Weak . create n ; } ) ; ;
let gccount ( ) = ( Gc . quick_stat ( ) ) . Gc . major_collections ; ;
let check_and_change i j = let gc1 = gccount ( ) in match data . ( i ) . objs . ( j ) , Weak . check data . ( i ) . wp j with | Present x , false -> assert false | Absent n , true -> assert ( gc1 <= n + 1 ) | Absent _ , false -> let x = Array . make ( 1 + Rando...
let dummy = ref [ ] ; ; || dummy := Array . make ( Random . int 300 ) 0 ; let i = Random . int size in let j = Random . int ( Array . length data . ( i ) . objs ) in check_and_change i j ; done
let alive = ref ( Array . init n ( fun _ -> Array . make 10 0 ) )
let create_weaks ( ) = Array . init n ( fun i -> let w = Weak . create 1 in Weak . set w 0 ( Some ( ! alive . ( i ) ) ) ; w )
let weak1 = create_weaks ( )
let weak2 = create_weaks ( )
let weak3 = create_weaks ( )
let ( ) = let dummy = ref [ ] || in for l = 0 to 10 do dummy := Array . make 300 0 done
let gccount ( ) = ( Gc . quick_stat ( ) ) . Gc . major_collections ; ;
let ( ) = for _l = 1 to loop do let bad = ref 0 in for i = 0 to n - 1 do for _j = 0 to n * 10 do ignore ( Weak . get weak2 . ( i ) 0 ) ; done ; if Weak . check weak2 . ( i ) 0 && not ( Weak . check weak1 . ( i ) 0 ) && Weak . check weak2 . ( i ) 0 the...
let random_state = Domain . DLS . new_key Random . State . make_self_init
let random_int = Random . State . int ( Domain . DLS . get random_state )
type block = int array ; ;
type objdata = | Present of block | Absent of int ; ;