text
stringlengths
0
601k
let add_superpressure_regs op live_regs res_regs spilled = let max_pressure = Proc . max_register_pressure op in let regs = Reg . add_set_array live_regs res_regs in let pressure = Array . make Proc . num_register_classes 0 in Reg . Set . iter ( fun r -> if Reg . Set . mem r spilled then ( ) else begin match r . loc wi...
let destroyed_at_fork = ref ( [ ] : ( instruction * Reg . Set . t ) list )
let add_reloads regset i = Reg . Set . fold ( fun r i -> instr_cons ( Iop Ireload ) [ | spill_reg r ] | [ | r ] | i ) regset i
let reload_at_exit = ref [ ]
let find_reload_at_exit k = try List . assoc k ! reload_at_exit with | Not_found -> Misc . fatal_error " Spill . find_reload_at_exit "
let rec reload i before = incr current_date ; record_use i . arg ; record_use i . res ; match i . desc with Iend -> ( i , before ) | Ireturn | Iop ( Itailcall_ind _ ) | Iop ( Itailcall_imm _ ) -> ( add_reloads ( Reg . inter_set_array before i . arg ) i , Reg . Set . empty ) | Iop ( Icall_ind _ | Icall_imm _ | Iextcall ...
let spill_at_exit = ref [ ]
let find_spill_at_exit k = try let used , set = List . assoc k ! spill_at_exit in used := true ; set with | Not_found -> Misc . fatal_error " Spill . find_spill_at_exit "
let spill_at_raise = ref Reg . Set . empty
let inside_loop = ref false
let add_spills regset i = Reg . Set . fold ( fun r i -> instr_cons ( Iop Ispill ) [ | r ] | [ | spill_reg r ] | i ) regset i
let rec spill i finally = match i . desc with Iend -> ( i , finally ) | Ireturn | Iop ( Itailcall_ind _ ) | Iop ( Itailcall_imm _ ) -> ( i , Reg . Set . empty ) | Iop Ireload -> let ( new_next , after ) = spill i . next finally in let before1 = Reg . diff_set_array after i . res in ( instr_cons i . desc i . arg i . res...
let reset ( ) = spill_env := Reg . Map . empty ; use_date := Reg . Map . empty ; current_date := 0 ; destroyed_at_fork := [ ]
let fundecl f = reset ( ) ; let ( body1 , _ ) = reload f . fun_body Reg . Set . empty in let ( body2 , tospill_at_entry ) = spill body1 Reg . Set . empty in let new_body = add_spills ( Reg . inter_set_array tospill_at_entry f . fun_args ) body2 in spill_env := Reg . Map . empty ; use_date := Reg . Map . empty ; destroy...
type t = [ ` Missing_env_var of string | ` Failed_to_parse of string * string | ` Invalid_template of string * string | ` Failed_to_generate of string ]
let to_string = function | ` Missing_env_var s -> Printf . sprintf " The environment variable " \% s " \ is needed , but could not be found in \ your environment . \ n \ n \ Hint : Try setting it and run the program again . " s | ` Failed_to_parse ( file , reason ) reason -> Printf . sprintf " I failed to parse the fil...
let missing_env env = ` Missing_env_var env
let failed_to_parse ~ msg file = ` Failed_to_parse ( file , msg ) msg
let failed_to_generate msg = ` Failed_to_generate msg
let invalid_template ~ msg template = ` Invalid_template ( template , msg ) msg
let of_decoder_error ~ file e = let msg = Decoder . string_of_error e in failed_to_parse file ~ msg
let sexp_of_t t = let open Sexplib . Sexp in match t with | ` Missing_env_var e -> List [ Atom " Missing_env_var " ; Atom e ] | ` Failed_to_parse ( e1 , e2 ) e2 -> List [ Atom " Missing_env_var " ; Atom e1 ; Atom e2 ] | ` Invalid_template ( e1 , e2 ) e2 -> List [ Atom " Missing_env_var " ; Atom e1 ; Atom e2 ] | ` Faile...
let pp fmt t = Sexplib . Sexp . pp_hum fmt ( sexp_of_t t ) t
module type Template = sig val name : string val file_list : string list val read : string -> string option end
module Cli : Template = struct include Cli let name = " cli " end
module Lib : Template = struct include Lib let name = " lib " end
module Bin : Template = struct include Bin let name = " bin " end
module Ppx : Template = struct include Ppx let name = " ppx " end
module C_bindings : Template = struct include C_bindings let name = " c - bindings " end
module Js : Template = struct include Js let name = " js " end
module Hello : Template = struct include Hello let name = " hello " end
let hello : ( module Template ) Template = ( module Hello ) Hello
let all : ( module Template ) Template list = [ ( module Cli ) Cli ; ( module Lib ) Lib ; ( module Bin ) Bin ; ( module Ppx ) Ppx ; ( module C_bindings ) C_bindings ; ( module Js ) Js ]
type content_leaf = { array : int array ; string : string }
type content = | CLeaf of content_leaf | CNode of content * content
type tree = | Empty | Node of ( tree * float * content * tree )
let rec splay_ ( ( left , key , value , right ) as a ) k = if k = key then a else if k < key then match left with | Empty -> a | Node ( lleft , lk , lv , lright ) -> ( if k = lk then lleft , lk , lv , Node ( lright , key , value , right ) else if k < lk then match lleft with | Empty -> Empty , lk , lv , Node ( lright ,...
let splay t key = match t with | Empty -> t | Node n -> Node ( splay_ n key )
let insert key value t = let t = splay t key in match t with | Empty -> Node ( Empty , key , value , Empty ) | Node ( left , rk , rv , right ) -> if rk = key then t else if key > rk then Node ( Node ( left , rk , rv , Empty ) , key , value , right ) else Node ( left , key , value , Node ( Empty , rk , rv , right ) )
let remove key t = let t = splay t key in match t with | Empty -> t | Node ( _ , rk , _ , _ ) when rk <> key -> raise Not_found | Node ( Empty , _ , _ , right ) -> right | Node ( left , _ , _ , right ) -> ( match splay left key with | Node ( lleft , lk , lv , Empty ) -> Node ( lleft , lk , lv , right ) | _ -> failwith ...
let find key t = let t = splay t key in match t with | Node ( _ , k , v , _ ) when k = key -> Some v , t | _ -> None , t
let rec findMax = function | Empty -> raise Not_found | Node ( _ , k , _ , Empty ) -> k | Node ( _ , _ , _ , right ) -> findMax right
let findGreatestLessThan key t = let t = splay t key in match t with | Empty -> None , t | Node ( _ , k , _ , _ ) when k < key -> Some k , t | Node ( Empty , _ , _ , _ ) -> None , t | Node ( left , _ , _ , _ ) -> Some ( findMax left ) , t
let exportKeys t = let rec aux l length = function | Empty -> l , length | Node ( left , k , _ , right ) -> let l , length = aux l length right in aux ( k :: l ) ( length + 1 ) left in aux [ ] 0 t
let rec generatePayloadTree depth tag = if depth = 0 then CLeaf { array = [ | 0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ] | ; string = " String for key " ^ tag ^ " in leaf node " } else CNode ( generatePayloadTree ( depth - 1 ) tag , generatePayloadTree ( depth - 1 ) tag )
let random = let seed = ref 49734321 in fun ( ) -> let s = ! seed in let s = ( s + 0x7ed55d16 + ( s lsl 12 ) ) land 0xffffffff in let s = s lxor 0xc761c23c lxor ( s lsr 19 ) in let s = s + 0x165667b1 + ( s lsl 5 ) in let s = ( s + 0xd3a2646c ) lxor ( s lsl 9 ) in let s = ( s + 0xfd7046c5 + ( s lsl 3 ) ) land 0xffffffff...
let insertNewNode t = let rec aux t = let key = generateKey ( ) in let vo , t = find key t in match vo with | None -> key , t | _ -> aux t in let key , t = aux t in let payload = generatePayloadTree kSplayTreePayloadDepth ( string_of_float key ) in key , insert key payload t
let splaySetup ( ) = let rec aux i t = if i < kSplayTreeSize then aux ( i + 1 ) ( snd ( insertNewNode t ) ) else t in aux 0 Empty
let splayTearDown t = let keys , length = exportKeys t in if length <> kSplayTreeSize then failwith " Splay tree has wrong size " ; match keys with | [ ] -> ( ) | a :: l -> ignore ( List . fold_left ( fun b e -> if b >= e then failwith " Splay tree not sorted " else e ) a l )
let splayRun t = let rec aux i t = if i < kSplayTreeModifications then let key , t = insertNewNode t in aux ( i + 1 ) ( match findGreatestLessThan key t with | None , t -> remove key t | Some k , t -> remove k t ) else t in aux 0 t
let ( ++ ) a b = b a
let ( ) = splaySetup ( ) ++ splayRun ++ splayTearDown
let body_lookups = let offset = function | 2 -> 0 . , 3 . 5 , - 5 . | 3 -> 1 . , - 2 . 5 , 0 . 5 | i when i >= 4 -> 0 . 5 , - 18 . , 8 . 5 | 0 -> - 2 . 5 , 0 . , 5 . | _ -> 0 . , 0 . , 0 . and curve = function | i when i >= 3 -> Curvature . ( curve ~ well ( : spec ~ radius : 37 . ( Float . pi . / 4 . 25 ) ) ( ) ) | i w...
let thumb_lookups = let curve _ = Curvature . ( curve ~ fan { : angle = Float . pi . / 9 . ; radius = 70 . ; tilt = Float . pi . / 48 . } ~ well { : angle = Float . pi . / 7 . 5 ; radius = 47 . ; tilt = 0 . } ( ) ) in Plate . Lookups . thumb ~ curve ( )
let plate_builder = Plate . make ~ n_body_cols : 5 ~ body_lookups ~ thumb_lookups ~ thumb_offset ( :- 13 . , - 41 . , 10 . ) ~ thumb_angle : Float . ( pi . / 40 . , pi . / - 14 . , pi . / 24 . ) ~ rotate_thumb_clips : false ~ caps : Caps . Matty3 . row ~ thumb_caps : Caps . MT3 . ( fun i -> if i = 1 then space_1_25u el...
let wall_builder plate = let eyelet_config = Eyelet . magnet_6x3_config in Walls . { body = auto_body ~ n_steps ( ` : Flat 3 ) ~ north_clearance : 2 . 5 ~ south_clearance : 2 . 5 ~ side_clearance : 1 . 5 ~ eyelet_config plate ; thumb = auto_thumb ~ south_lookup ( : fun _ -> Yes ) ~ east_lookup ( : fun _ -> No ) ~ west_...
let base_connector = Connect . skeleton ~ n_facets : 1 ~ height : 9 . ~ index_height : 12 . ~ thumb_height : 12 . ~ east_link ( : Connect . snake ~ height : 12 . ~ scale : 1 . 5 ~ d : 1 . 2 ( ) ) ~ west_link ( : Connect . straight ~ height : 12 . ( ) ) ~ cubic_d : 2 . ~ cubic_scale : 1 . 5 ~ body_join_steps ( ` : Flat ...
let plate_welder plate = Scad . union [ Plate . skeleton_bridges plate ; Bridge . cols ~ columns : plate . body 1 2 ]
let ports_cutter = BastardShield . ( cutter ~ x_off : 0 . ~ y_off ( :- 1 . ) ( make ( ) ) )
let build ? right_hand ? hotswap ( ) = Case . make ? right_hand ~ plate_builder ~ plate_welder ~ wall_builder ~ base_connector ~ ports_cutter ( Mx . make_hole ? hotswap ~ clearance : 2 . ( ) )
let bottom ( ? chonk = false ) case = let fastener = Option . some_if ( not chonk ) @@ Eyelet . Magnet { rad = 2 . 65 ; thickness = 1 . 2 } in Bottom . make ? fastener case
let tent case = Tent . make ~ degrees : 30 . case
let bastard_compare ( ) = Scad . union [ Skeletyl . bastard_skelly ; Case . to_scad ~ show_caps : false ( build ( ) ) |> Scad . color ~ alpha : 0 . 5 Color . Yellow ]
type output_mode = | Dot | OCaml | Promela | GUI | Debug
let main = let mode = ref default_output_mode in let optimise = ref default_optimisation in let log_level = ref default_log_level in let files = ref [ ] in let ofile = ref None in let statecall_fname = ref None in let html_dir = ref " . " in let debug = ref false in let parse = [ " - O " , Arg . Int ( fun x -> if x = 0...
type ' a t = D . env -> ' a TB . m * D . env
let foreign con k : _ t = fun env -> let env ' = { env with conenv = env . conenv <>< [ con ] } in let var = TB . lvl @@ BwdLabels . length env . conenv in k var env '
let foreign_cof phi = foreign @@ D . cof_to_con phi
let foreign_dim r = foreign @@ D . dim_to_con r
let foreign_clo clo = foreign @@ D . Lam ( ` Anon , clo )
let foreign_tp tp k : _ t = fun env -> let env ' = { env with tpenv = env . tpenv <>< [ tp ] } in let var = TB . tplvl @@ BwdLabels . length env . tpenv in k var env '
let foreign_list ( cons : D . con list ) k : _ t = let rec go cons k = match cons with | [ ] -> k [ ] | con :: cons -> foreign con @@ fun tm -> go cons @@ fun tms -> k @@ tm :: tms in go cons k
let compile ( t : ' a t ) : D . env * ' a = let m , env = t { tpenv = Emp ; conenv = Emp } in let tplen = BwdLabels . length env . tpenv in let conlen = BwdLabels . length env . conenv in env , TB . run ~ tplen ~ conlen m
let term ( m : ' a TB . m ) : ' a t = fun env -> m , env
module F = struct let con = foreign let tp = foreign_tp let cons = foreign_list let dim = foreign_dim let cof = foreign_cof let clo = foreign_clo end
module Macro = struct let commute_split ( self : D . con ) ( phis : D . cof list ) ( action : ' a TB . m -> ' b TB . m ) = let phis = List . map D . cof_to_con phis in F . con self @@ fun self -> F . cons phis @@ fun phis -> term @@ TB . cof_split @@ List . map ( fun phi -> phi , action self ) phis let tp_pequiv_in_v ~...
module Bdry = struct module CB = CofBuilder let cap ~ r ~ r ' ~ phi ~ code ~ box = CB . join [ CB . eq r r ' ; phi ] , F . dim r @@ fun r -> F . dim r ' @@ fun r ' -> F . cof phi @@ fun phi -> foreign code @@ fun code -> foreign box @@ fun box -> term @@ TB . cof_split [ TB . eq r r ' , box ; phi , TB . coe code r ' r ...
type subst = Reg . t Reg . Map . t
let subst_reg r ( sub : subst ) = try Reg . Map . find r sub with Not_found -> r
let subst_regs rv sub = match sub with None -> rv | Some s -> let n = Array . length rv in let nv = Array . make n Reg . dummy in for i = 0 to n - 1 do nv . ( i ) <- subst_reg rv . ( i ) s done ; nv
let equiv_classes = ref ( Reg . Map . empty : Reg . t Reg . Map . t )
let rec repres_reg r = try repres_reg ( Reg . Map . find r ! equiv_classes ) with Not_found -> r
let repres_regs rv = let n = Array . length rv in for i = 0 to n - 1 do rv . ( i ) <- repres_reg rv . ( i ) done
let identify r1 r2 = let repres1 = repres_reg r1 in let repres2 = repres_reg r2 in if repres1 . stamp = repres2 . stamp then ( ) else begin equiv_classes := Reg . Map . add repres1 repres2 ! equiv_classes end
let identify_sub sub1 sub2 reg = try let r1 = Reg . Map . find reg sub1 in try let r2 = Reg . Map . find reg sub2 in identify r1 r2 with Not_found -> identify r1 reg with Not_found -> try let r2 = Reg . Map . find reg sub2 in identify r2 reg with Not_found -> ( )
let merge_substs sub1 sub2 i = match ( sub1 , sub2 ) with ( None , None ) -> None | ( Some _ , None ) -> sub1 | ( None , Some _ ) -> sub2 | ( Some s1 , Some s2 ) -> Reg . Set . iter ( identify_sub s1 s2 ) ( Reg . add_set_array i . live i . arg ) ; sub1
let merge_subst_array subv instr = let rec find_one_subst i = if i >= Array . length subv then None else begin match subv . ( i ) with None -> find_one_subst ( i + 1 ) | Some si as sub -> for j = i + 1 to Array . length subv - 1 do match subv . ( j ) with None -> ( ) | Some sj -> Reg . Set . iter ( identify_sub si sj )...
let exit_subst = ref [ ]
let find_exit_subst k = try List . assoc k ! exit_subst with | Not_found -> Misc . fatal_error " Split . find_exit_subst "
let rec rename i sub = match i . desc with Iend -> ( i , sub ) | Ireturn | Iop ( Itailcall_ind _ ) | Iop ( Itailcall_imm _ ) -> ( instr_cons_debug i . desc ( subst_regs i . arg sub ) [ ] || i . dbg i . next , None ) | Iop Ireload when i . res . ( 0 ) . loc = Unknown -> begin match sub with None -> rename i . next sub |...
let set_repres i = instr_iter ( fun i -> repres_regs i . arg ; repres_regs i . res ) i
let reset ( ) = equiv_classes := Reg . Map . empty ; exit_subst := [ ]
let fundecl f = reset ( ) ; let new_args = Array . copy f . fun_args in let ( new_body , _sub_body ) = rename f . fun_body ( Some Reg . Map . empty ) in repres_regs new_args ; set_repres new_body ; equiv_classes := Reg . Map . empty ; { fun_name = f . fun_name ; fun_args = new_args ; fun_body = new_body ; fun_codegen_o...
let is_odd x = x lor 1L = x
module State = struct type t = { mutable seed : int64 ; odd_gamma : int64 } let golden_gamma = 0x9e37_79b9_7f4a_7c15L let of_int seed = { seed = Int64 . of_int seed ; odd_gamma = golden_gamma } let copy { seed ; odd_gamma } = { seed ; odd_gamma } let mix_bits z n = z lxor ( z lsr n ) let mix64 z = let z = ( mix_bits z ...
let bool state = is_odd ( State . next_int64 state )
let remainder_is_unbiased ~ draw ~ remainder ~ draw_maximum ~ remainder_maximum = let open Int64 . O in draw - remainder <= draw_maximum - remainder_maximum let draw_maximum = 104L in let remainder_maximum = 9L in let is_unbiased draw = let remainder = Int64 . rem draw ( Int64 . succ remainder_maximum ) in remainder_is...
let int64 = let open Int64 . O in let rec between state ~ lo ~ hi = let draw = State . next_int64 state in if lo <= draw && draw <= hi then draw else between state ~ lo ~ hi in let rec non_negative_up_to state maximum = let draw = State . next_int64 state land Int64 . max_value in let remainder = Int64 . rem draw ( Int...
let int state ~ lo ~ hi = let lo = Int64 . of_int lo in let hi = Int64 . of_int hi in Int64 . to_int_trunc ( int64 state ~ lo ~ hi )
let int32 state ~ lo ~ hi = let lo = Int64 . of_int32 lo in let hi = Int64 . of_int32 hi in Int64 . to_int32_trunc ( int64 state ~ lo ~ hi )