text
stringlengths
0
601k
type buffer = { data : Buffer . t ; mutable index : int ; size : int }
let get ( buffer : buffer ) : char = let c = Buffer . nth buffer . data buffer . index in buffer . index <- buffer . index + 1 ; c
let get_int ( buffer : buffer ) : int32 = Int32 . of_int ( Char . code ( get buffer ) )
let shift_or ( b1 : int32 ) ( b2 : int32 ) : int32 = Int32 . logor ( Int32 . shift_left b2 8 ) b1
let read2 ( buffer : buffer ) : int32 = let b1 = get_int buffer in let b2 = get_int buffer in shift_or b1 b2
let read3 ( buffer : buffer ) : int32 = let b1 = get_int buffer in let b2 = get_int buffer in let b3 = get_int buffer in b3 |> shift_or b2 |> shift_or b1
let read4 ( buffer : buffer ) : int32 = let b1 = get_int buffer in let b2 = get_int buffer in let b3 = get_int buffer in let b4 = get_int buffer in b4 |> shift_or b3 |> shift_or b2 |> shift_or b1
let read4_chars ( buffer : buffer ) : string = let c1 = get buffer in let c2 = get buffer in let c3 = get buffer in let c4 = get buffer in let result = Bytes . create 4 in Bytes . set result 0 c1 ; Bytes . set result 1 c2 ; Bytes . set result 2 c3 ; Bytes . set result 3 c4 ; Bytes . to_string result
let searchData ( buffer : buffer ) : bool = let rec skipData n = if n = 0 then ( ) else let _ = get buffer in skipData ( n - 1 ) in let rec loop ( ) = if read4_chars buffer = " data " then true else let size = read4 buffer |> Int32 . to_int in let ( ) = skipData size in loop ( ) in match loop ( ) with | found -> found ...
let max_16 = ( 2 . 0 ** 16 . 0 ) . / 2 . 0
let sign_16 = Int32 . shift_left Int32 . one 15
let mask_16 = Int32 . shift_left Int32 . minus_one 15
let readSample16 ( buffer : buffer ) : float = let v = read2 buffer in if Int32 . logand sign_16 v <> Int32 . zero then Int32 . to_float ( Int32 . logor mask_16 v ) . / max_16 else Int32 . to_float v . / max_16
let max_24 = ( 2 . 0 ** 24 . 0 ) . / 2 . 0
let sign_24 = Int32 . shift_left Int32 . one 23
let mask_24 = Int32 . shift_left Int32 . minus_one 23
let readSample24 ( buffer : buffer ) : float = let v = read2 buffer in if Int32 . logand sign_24 v <> Int32 . zero then Int32 . to_float ( Int32 . logor mask_24 v ) . / max_24 else Int32 . to_float v . / max_24
let getReadSampleFunction ( bits : int32 ) : ( buffer -> float , string ) result = match Int32 . to_int bits with | 16 -> Ok readSample16 | 24 -> Ok readSample24 | _ -> Error ( " Wave file encoded in an unsupported bits per sample : " ^ string_of_int ( Int32 . to_int bits ) )
let readSamples ( buffer : buffer ) ( channels : int ) ( size : int ) ( data : float array array ) ( read_fn : buffer -> float ) : int = let rec loop_channels index channel = if channel >= channels then true else try let value = read_fn buffer in let channel_data = data . ( channel ) in let ( ) = channel_data . ( index...
let checkFormat ( buffer : buffer ) = if not ( read4_chars buffer = " RIFF " ) then Error " Not a valid file " else let chunk_size = read4 buffer in if chunk_size < Int32 . of_int 4 then Error " Invalid chunk size " else if not ( read4_chars buffer = " WAVE " ) then Error " Not a supported wav file " else if not ( read...
type wave = { channels : int ; samples : int ; data : float array array }
let read ( file : string ) : ( wave , string ) result = match FileIO . read_bytes file with | None -> Error " failed to open the file " | Some data -> let buffer = { index = 0 ; size = Buffer . length data ; data } in ( match checkFormat buffer with | Error _ as error -> error | Ok ( ) -> let channels = read2 buffer |>...
let callback fn = object inherit [ _ ] Wayland_client . Wl_callback . v1 method on_done ~ callback_data = fn callback_data end
let chars = ref 0
type state = Inside_word | Outside_word
let count_channel in_channel = let rec count status = let c = input_char in_channel in incr chars ; match c with ' \ n ' -> incr lines ; count Outside_word | ' ' | ' \ t ' -> count Outside_word | _ -> if status = Outside_word then begin incr words ; ( ) end ; count Inside_word in try count Outside_word with End_of_file...
let count_file name = let ic = open_in name in count_channel ic ; close_in ic
let print_result ( ) = print_int ! chars ; print_string " characters , " ; print_int ! words ; print_string " words , " ; print_int ! lines ; print_string " lines " ; print_newline ( )
let count name = count_file name ; print_result ( )
let _ = try if Array . length Sys . argv <= 1 then count_channel stdin else for i = 1 to Array . length Sys . argv - 1 do count_file Sys . argv . ( i ) done ; print_result ( ) print_string " I / O error : " ; print_string s ; print_newline ( )
type structured_code = { functions : func_block IMap . t ; } and func_block = { scope : cfg_scope ; block : block ; } and block = { loop_label : int option ; instructions : instruction array ; break_label : int option ; } and instruction = | Block of block | Label of int | Simple of I . instruction | Trap of { trylabel...
type trap_info = | Trap_push of int * int option | Trap_pop of int * int
type try_info = | Try_entry of int | Try_exit
type cfg_node = { cfg_scope : cfg_scope ; cfg_node_label : int ; cfg_try : try_info option ; cfg_trap : trap_info option ; mutable cfg_loops : int list ; cfg_succ : int list ; cfg_length : int ; cfg_final : I . instruction option ; cfg_next_main : int option ; }
type cfg = { mutable nodes : cfg_node IMap . t ; mutable code : I . instruction array ; mutable labels : ISet . t ; }
let string_of_scope s = sprintf " [ letrec =% s , func =% d , try =% s ] " ( match s . cfg_letrec_label with | None -> " none " | Some l -> string_of_int l ) s . cfg_func_label ( List . map string_of_int s . cfg_try_labels |> String . concat " , " )
let detect_loops ctx = let visited = ref ISet . empty in let trails = ref IMap . empty in let rec recurse exectrail label = let node = try IMap . find label ctx . nodes with Not_found -> assert false in if not ( ISet . mem label ! visited ) then ( visited := ISet . add label ! visited ; trails := IMap . add label exect...
let func_labels instr = match instr with | I . Kclosure ( l , _ ) -> [ l ] | Kclosurerec ( ll , _ ) -> ll | _ -> [ ]
let jump_labels instr = match instr with | I . Kbranch l -> [ l ] | Kbranchif l -> [ l ] | Kbranchifnot l -> [ l ] | Kswitch ( la1 , la2 ) -> Array . to_list la1 @ Array . to_list la2 | Kpushtrap l -> [ l ] | _ -> [ ]
let not_continuing instr = match instr with | I . Kbranch _ -> true | Kreturn _ -> true | Kappterm _ -> true | Kswitch _ -> true | Kraise _ -> true | Kstop -> true | _ -> false
let is_trapping instr = match instr with | I . Kpushtrap _ -> true | _ -> false
let create_cfg code labels = let labels = ref labels in let max_label = ref ( Array . length code - 1 ) in let new_label ( ) = let label = ! max_label + 1 in labels := ISet . add label ! labels ; max_label := label ; label in let nodes = Hashtbl . create 7 in let queued = Hashtbl . create 7 in let pushtraps = ref IMap ...
let split_main_function cfg = let depth_table = Hashtbl . create 7 in let excluded = Hashtbl . create 7 in let exclude p = Hashtbl . replace excluded p true in let rec set_depth depth label = if not ( Hashtbl . mem depth_table label ) then ( Hashtbl . add depth_table label depth ; let node = try IMap . find label cfg ....
let is_node_in_loop ctx loop_label label = let node = try IMap . find label ctx . nodes with Not_found -> assert false in List . mem loop_label node . cfg_loops
let recover_structure ctx = let in_degree = ref IMap . empty in let rec inc_degree trail label = let deg = try IMap . find label ! in_degree with Not_found -> 0 in in_degree := IMap . add label ( deg + 1 ) ! in_degree ; if deg = 0 then ( let node = try IMap . find label ctx . nodes with Not_found -> assert false in let...
let validate scode = let error func_label last_label message = failwith ( sprintf " validation error function % d near % d : % s " func_label last_label message ) in let rec validate_block labels_in_scope func_label last_label block = if block . loop_label <> None && block . break_label <> None then error func_label la...
let prefix = Sys . getenv " HOME " ^ " . / wasicaml "
let size_of_function func = let rec size_of_block block = Array . fold_left ( fun acc instr -> match instr with | Block b -> acc + size_of_block b | Label _ -> acc | _ -> acc + 1 ) 0 block . instructions in size_of_block func . block
let size_table s get_defname = let get_letrec_name func = let label = Option . value ~ default : 0 func . scope . cfg_letrec_label in let prefix = if label = 0 then " letrec_main " else if func . scope . cfg_main then sprintf " letrec_main % d " label else sprintf " letrec % d " label in try prefix ^ " _ " ^ get_defnam...
let main ( ) = let out = ref " a . out " in let inp = ref None in let cclib = ref [ ] in let cstack = ref ( 1024 * 1024 ) in let quiet = ref false in Arg . parse [ " - o " , Arg . Set_string out , " < file > Set the output file " ; " - cclib " , Arg . String ( fun s -> cclib := ! cclib @ [ s ] ) , " < option > pass thi...
let ( ) = try main ( ) with | Failure msg -> eprintf " % s \ n " %! msg ; exit 2 | Arg . Bad msg -> eprintf " % s \ n " %! msg ; exit 2
type wasm_value_type = | TI32 | TI64 | TF64
let string_of_vtype = function | TI32 -> " i32 " | TI64 -> " i64 " | TF64 -> " f64 "
let zero_expr_of_vtype = function | TI32 -> [ L [ K " i32 . const " ; N ( I32 0l ) ] ] | TI64 -> [ L [ K " i64 . const " ; N ( I64 0L ) ] ] | TF64 -> [ L [ K " f64 . const " ; N ( F64 0 . 0 ) ] ]
type letrec_label = | Func of int | Main of int
type gpad = { letrec_name : ( letrec_label , string ) Hashtbl . t ; primitives : ( string , sexp list ) Hashtbl . t ; funcmapping : ( int , letrec_label * int ) Hashtbl . t ; subfunctions : ( letrec_label , int list ) Hashtbl . t ; wasmindex : ( letrec_label , int ) Hashtbl . t ; mutable need_reinit_frame : bool ; muta...
type fpad = { lpad : Wc_unstack . lpad ; fpad_letrec_label : letrec_label ; mutable fpad_scope : Wc_control . cfg_scope ; mutable maxdepth : int ; mutable need_appterm_common : bool ; mutable need_return : bool ; mutable need_panic : bool ; mutable need_tmp1_i32 : bool ; mutable need_tmp2_i32 : bool ; mutable need_tmp1...
let enable_multireturn = ref false
let enable_deadbeef_check = ref false
let make_header size tag = ( size lsl 10 ) lor tag
let vtype repr = match repr with | RValue | RInt | RIntUnclean | RIntVal | RNatInt | RInt32 -> TI32 | RInt64 -> TI64 | RFloat -> TF64
let empty_fpad ( ) = { lpad = Wc_unstack . empty_lpad ( ) ; fpad_letrec_label = Main 0 ; fpad_scope = { cfg_letrec_label = None ; cfg_func_label = 0 ; cfg_try_labels = [ ] ; cfg_main = false } ; maxdepth = 0 ; need_appterm_common = false ; need_return = false ; need_panic = false ; need_tmp1_i32 = false ; need_tmp2_i32...
let new_local fpad repr = Wc_unstack . new_local fpad . lpad repr
let req_tmp1_i32 fpad = if fpad . lpad . avoid_locals then ( fpad . need_tmp1_i32 <- true ; " tmp1_i32 " ) else new_local fpad RInt
let req_tmp2_i32 fpad = if fpad . lpad . avoid_locals then ( fpad . need_tmp2_i32 <- true ; " tmp2_i32 " ) else new_local fpad RInt
let req_tmp1_f64 fpad = if fpad . lpad . avoid_locals then ( fpad . need_tmp1_f64 <- true ; " tmp1_f64 " ) else new_local fpad RFloat
let set_bp_1 fpad = [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * fpad . maxdepth ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " local . tee " ; ID " bp " ] ; L [ K " global . get " ; ID " wasicaml_stack_threshold " ] ; L [ K " i32 . lt_u " ] ; L [ K " if " ; L [ K " then " ; L [ K " call " ; ID " caml_raise_stac...
let set_bp fpad = if fpad . maxdepth = 0 then [ ] else [ L [ K " local . get " ; ID " fp " ] ] @ set_bp_1 fpad
let push_const n = [ L [ K " i32 . const " ; N ( I32 n ) ] ]
let push_local var = [ L [ K " local . get " ; ID var ] ]
let pop_to_local var = [ L [ K " local . set " ; ID var ] ]
let pop_to_fp fpad = if fpad . maxdepth = 0 then pop_to_local " fp " else [ L [ K " local . tee " ; ID " fp " ] ] @ set_bp_1 fpad
let load_offset offset = if offset >= 0 then [ L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int offset ) ) ; K " align = 2 " ; ] ] else [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( - offset ) ) ) ] ; L [ K " i32 . sub " ] ; L [ K " i32 . load " ; K " align = 2 " ] ]
let add_offset offset = if offset <> 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int offset ) ) ] ; L [ K " i32 . add " ] ] else [ ]
let push_env = [ L [ K " local . get " ; ID " envptr " ] ; L [ K " i32 . load " ; K " align = 2 " ] ]
let push_field var_base field = [ L [ K " local . get " ; ID var_base ; ] ] @ load_offset ( 4 * field )
let push_global_field var_base field = [ L [ K " global . get " ; ID var_base ; ] ] @ load_offset ( 4 * field )
let push_field_addr var_base field = [ L [ K " local . get " ; ID var_base ] ] @ if field <> 0 then [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * field ) ) ) ; ] ; L [ K " i32 . add " ] ] else [ ]
let push_global_field_addr var_base field = [ L [ K " global . get " ; ID var_base ; ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * field ) ) ) ; ] ; L [ K " i32 . add " ] ]
let push_stack fpad pos = if pos >= 0 then push_field " fp " pos else push_field " bp " ( pos + fpad . maxdepth )
let push_domain_field field = [ L [ K " global . get " ; ID " wasicaml_domain_state " ] ; L [ K " i32 . load " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * field ) ) ) ; K " align = 2 " ; ] ; ]
let store_offset addr offset code_value = if offset >= 0 then [ L [ K " local . get " ; ID addr ] ] @ code_value @ [ L [ K " i32 . store " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int offset ) ) ; K " align = 2 " ; ] ] else [ L [ K " local . get " ; ID addr ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( ...
let pop_to_field var_base field code_value = store_offset var_base ( 4 * field ) code_value
let pop_to_double_field var_base field code_value = [ L [ K " local . get " ; ID var_base ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * double_size * field ) ) ) ; ] ; L [ K " i32 . add " ] ] @ code_value @ [ L [ K " f64 . store " ; K " align = 2 " ] ]
let pop_to_domain_field field code_value = [ L [ K " global . get " ; ID " wasicaml_domain_state " ] ] @ code_value @ [ L [ K " i32 . store " ; K ( sprintf " offset = 0x % lx " ( Int32 . of_int ( 8 * field ) ) ) ; K " align = 2 " ; ] ; ]
let pop_to_stack fpad pos code_value = if pos >= 0 then pop_to_field " fp " pos code_value else pop_to_field " bp " ( pos + fpad . maxdepth ) code_value
let load_double = [ L [ K " f64 . load " ; K " align = 2 " ] ]
let debug2 x0 x1 = [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int x0 ) ) ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int x1 ) ) ] ; L [ K " call " ; ID " debug2 " ] ]
let debug2_var x0 var = [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int x0 ) ) ] ; L [ K " local . get " ; ID var ] ; L [ K " call " ; ID " debug2 " ] ]
let deadbeef_init = [ L ( [ [ K " func " ; ID " deadbeef_init " ; L [ K " param " ; ID " bp " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; ] ; [ L [ K " block " ; ID " loop_exit " ; BR ; L [ K " loop " ; ID " loop " ; BR ; L [ K " local . get " ; ID " bp " ] ; L [ K " local . get " ; ID " fp " ] ; L [ K ...
let deadbeef_check = [ L ( [ [ K " func " ; ID " deadbeef_check " ; L [ K " param " ; ID " ptr " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; ] ; push_local " ptr " ; push_local " fp " ; [ L [ K " i32 . gt_u " ] ; L [ K " if " ; L [ K " then " ; L [ K " unreachable " ] ] ] ] ; [ L [ K " block " ; ID " lo...
let stack_init fpad descr = List . map ( fun pos -> pop_to_stack fpad pos ( push_const 1l ) ) descr . stack_uninit |> List . flatten
let setup_for_gc fpad descr = let sp_decr = if descr . stack_save_accu then 1 else 0 in let sexpl_stack = stack_init fpad descr in let sexpl_accu = if descr . stack_save_accu then push_local " accu " |> pop_to_stack fpad ( - descr . stack_depth - 1 ) else [ ] in let sexpl_extern_sp = ( [ L [ K " local . get " ; ID " fp...
let restore_after_gc fpad descr = if descr . stack_save_accu then push_stack fpad ( - descr . stack_depth - 1 ) @ pop_to_local " accu " else [ ]
let alloc_atom fpad tag = [ L [ K " global . get " ; ID " wasicaml_atom_table " ; ] ; L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( 4 * tag ) ) ) ; ] ; L [ K " i32 . add " ] ; ]
let alloc_fast = [ L ( [ [ K " func " ; ID " alloc_fast " ; L [ K " param " ; ID " bhsize " ; K " i32 " ] ; L [ K " param " ; ID " header " ; K " i32 " ] ; BR ; L [ K " result " ; C " ptr " ; K " i32 " ] ; L [ K " local " ; ID " ptr " ; K " i32 " ] ; ] ; push_domain_field domain_field_young_ptr ; push_local " bhsize " ...
let alloc_slow ( ) = [ L ( [ [ K " func " ; ID " alloc_slow " ; L [ K " param " ; ID " wosize " ; K " i32 " ] ; L [ K " param " ; ID " header " ; K " i32 " ] ; L [ K " param " ; ID " fp " ; K " i32 " ] ; L [ K " param " ; ID " stackdepth " ; K " i32 " ] ; L [ K " param " ; ID " accu " ; K " i32 " ] ; BR ; L [ K " resul...
let call_alloc_slow ( ) = [ L [ K " call " ; ID " alloc_slow " ] ] @ if ! enable_multireturn then [ ] else [ L [ K " global . get " ; ID " retval2 " ] ]
let alloc_non_atom fpad descr size tag = fpad . need_xalloc <- true ; let ptr = " xalloc " in let young = size <= max_young_wosize in let code = if young then [ push_const ( Int32 . of_int ( 4 * ( size + 1 ) ) ) ; push_const ( Int32 . of_int ( make_header size tag ) ) ; [ L [ K " call " ; ID " alloc_fast " ] ; L [ K " ...
let alloc fpad descr size tag = if size = 0 then alloc_atom fpad tag else let ( code , ptr , _ ) = alloc_non_atom fpad descr size tag in code @ push_local ptr