text
stringlengths
12
786k
type mb_elem = | MB_store of store | MB_const of int32 | MB_code of sexp list
let push_mb_elem fpad = function | MB_store src -> push_as fpad src RValue | MB_const n -> push_const n | MB_code code -> code
let makeblock fpad descr src_list tag = let size = List . length src_list in let sexpl_alloc , ptr , young = alloc_set fpad descr size tag in let sexpl_init = if young then List . mapi ( fun field src -> push_mb_elem fpad src |> pop_to_field ptr field ) src_list else List . mapi ( fun field src -...
let makefloatblock fpad descr src_list = let size = List . length src_list in let wosize = size * double_size in let sexpl_alloc , ptr , _ = alloc_set fpad descr wosize double_array_tag in let sexpl_init = List . mapi ( fun field src -> ( push_as fpad src RValue @ load_double ) |> pop_to_double_...
let lookup_label gpad lab = let letrec_label , subfunc = try Hashtbl . find gpad . funcmapping lab with Not_found -> assert false in let wasmindex = try Hashtbl . find gpad . wasmindex letrec_label with Not_found -> assert false in ( wasmindex , letrec_label , subfunc )
let push_wasmptr gpad lab = let wasmindex , letrec_label , subfunc = lookup_label gpad lab in [ L [ K " i32 . const " ; ID ( Hashtbl . find gpad . letrec_name letrec_label ) ] ]
let push_codeptr gpad lab = let wasmindex , letrec_label , subfunc = lookup_label gpad lab in push_wasmptr gpad lab @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int code_pointer_shift ) ) ] ; L [ K " i32 . shl " ] ; L [ K " i32 . const " ; N ( I32 ( Int32 ....
let closurerec gpad fpad descr src_list dest_list = let nfuncs = List . length dest_list in let envofs = nfuncs * 3 - 1 in let mb_src_list = ( List . mapi ( fun i ( _ , label ) -> ( if i > 0 then [ MB_const ( Int32 . of_int ( make_header ( 3 * i ) infix_tag ) ) ] else [ ...
let c_call gpad fpad descr src_list name = let descr = { descr with stack_save_accu = false } in let sexpl_setup = setup_for_gc fpad descr in let sexpl_restore = restore_after_gc fpad descr in let sexpl_args = List . map ( fun src -> push_as fpad src RValue ) src_list in let sexpl_call = [ L [ K "...
let c_call_vector gpad fpad descr numargs depth name = let descr = { descr with stack_save_accu = false } in let sexpl_setup = setup_for_gc fpad descr in let sexpl_restore = restore_after_gc fpad descr in let sexpl_call = push_field_addr " fp " ( - depth ) @ push_const ( Int32 . of_int numargs )...
let string_label = function | Label k -> sprintf " label % d " k | Loop k -> sprintf " loop % d " k
let switch fpad src labls_ints labls_blocks = fpad . need_panic <- true ; let value = req_tmp1_i32 fpad in push_as fpad src RValue @ pop_to_local value @ [ L [ K " local . get " ; ID value ] ; L [ K " i32 . const " ; N ( I32 1l ) ] ; L [ K " i32 . and " ] ; L [ ...
let grab fpad num = let sexpl = [ L [ K " local . get " ; ID " codeptr " ] ; L [ K " i32 . const " ; N ( I32 code_pointer_restart_mask ) ] ; L [ K " i32 . and " ] ; L [ K " if " ; L ( [ K " then " ; L [ K " local . get " ; ID " envptr " ...
let return = [ C " $ return " ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " if " ; L [ K " then " ; L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " local . get " ; ID " f...
let throw fpad = if fpad . fpad_scope . cfg_main && fpad . fpad_scope . cfg_try_labels = [ ] then [ L [ K " call " ; ID " wasicaml_throw " ] ; L [ K " unreachable " ] ] else if fpad . fpad_scope . cfg_try_labels = [ ] then [ L [ K " i32 . const " ; N ( I...
let apply_direct gpad fpad funlabel numargs depth = let _ , letrec_label , _ = lookup_label gpad funlabel in let letrec_name = Hashtbl . find gpad . letrec_name letrec_label in let env_pos = ( - depth + numargs + 1 ) in ( push_local " accu " |> pop_to_stack fpad env_pos ) @ push_field_ad...
let apply fpad numargs depth = let env_pos = ( - depth + numargs + 1 ) in let codeptr = req_tmp1_i32 fpad in ( push_local " accu " |> pop_to_stack fpad env_pos ) @ push_field_addr " fp " env_pos @ [ L [ K " i32 . const " ; N ( I32 ( Int32 . of_int ( numargs - 1 ) ) ) ...
let appterm_common fpad = [ C " $ appterm_common " ; L [ K " local . get " ; ID " envptr " ] ; L [ K " local . get " ; ID " codeptr " ] ; L [ K " local . get " ; ID " accu " ] ; L [ K " local . get " ; ID " extra_args " ] ; L [ K " lo...
let appterm gpad fpad numargs oldnumargs depth = let sexpl = if numargs <= 10 then ( gpad . need_reinit_frame_k <- ISet . add numargs gpad . need_reinit_frame_k ; push_local " fp " @ push_const ( Int32 . of_int depth ) @ push_const ( Int32 . of_int oldnumargs ) @ [ L [ K " call "...
let trap gpad fpad trylabel catchlabel depth = let local1 = req_tmp1_i32 fpad in let local2 = req_tmp2_i32 fpad in let sexpl_stack = ( push_const 0l |> pop_to_stack fpad ( - depth - 1 ) ) @ ( push_const 0l |> pop_to_stack fpad ( - depth - 2 ) ) @ ( push_const 0l |> pop_to_stack fpad ...
let rec emit_instr gpad fpad instr = match instr with | Wcomment s -> [ ] | Wblock arg -> ( match arg . label with | Label lab -> [ L ( [ K " block " ; ID ( sprintf " label % d " lab ) ; BR ] @ emit_instrs gpad fpad arg . body ) ] | Loop lab -> [ L ( [ K " loop "...
let emit_fblock gpad fpad fblock = let maxdepth = Wc_tracestack . max_stack_depth_of_fblock fblock in fpad . maxdepth <- if maxdepth > 0 then maxdepth + 2 else 0 ; let instrs = Wc_unstack . transl_fblock fpad . lpad fblock |> emit_instrs gpad fpad in set_bp fpad @ ( if ! enable_deadbeef_check &...
let get_funcmapping scode = let open Wc_control in let funcmapping = Hashtbl . create 7 in let subfunction_num = Hashtbl . create 7 in let subfunctions = Hashtbl . create 7 in IMap . iter ( fun func_label fblock -> let letrec_func_label = match fblock . scope . cfg_letrec_label with | None -> 0...
let block_cascade start_sexpl label_sexpl_pairs = let rec shift prev_sexpl pairs = match pairs with | ( label , lsexpl ) :: pairs ' -> ( prev_sexpl , Some label ) :: shift lsexpl pairs ' | [ ] -> [ prev_sexpl , None ] in let rec arrange inner_sexpl shifted = match shifted with | ( ...
let cond_section cond label sexpl_section sexpl_users = if cond then [ L ( [ K " block " ; ID label ; BR ] @ sexpl_users ) ] @ sexpl_section else sexpl_users
let cond_loop cond label sexpl = if cond then [ L ( [ K " loop " ; ID label ; BR ] @ sexpl ) ] else sexpl
let eff_label = function | Func l -> l | Main l -> l
let init_lpad_for_subfunc gpad fpad func_label = let func_offset , environment = ( try Hashtbl . find gpad . glbfun_table func_label with Not_found -> 0 , [ | ] | ) in fpad . lpad . environment <- environment ; fpad . lpad . func_offset <- func_offset
let generate_function scode gpad letrec_label func_name subfunc_labels export_flag = let fpad = { ( empty_fpad ( ) ) with fpad_letrec_label = letrec_label } in Hashtbl . add fpad . lpad . locals " accu " RValue ; Hashtbl . add fpad . lpad . locals " bp " RValue ; fpad . lpad . ...
let generate_letrec scode gpad letrec_label = let subfunc_labels = try Hashtbl . find gpad . subfunctions letrec_label with Not_found -> assert false in assert ( subfunc_labels <> [ ] ) ; let func_name = Hashtbl . find gpad . letrec_name letrec_label in let export = ( letrec_label = Main 0 )...
let globals ( ) = [ " wasicaml_global_data " , true , TI32 ; " wasicaml_domain_state " , true , TI32 ; " wasicaml_atom_table " , true , TI32 ; " wasicaml_stack_threshold " , true , TI32 ; " exn_result " , true , TI32 ; ] @ if ! enable_multireturn then [ ] ...
let imp_functions = [ " env " , " caml_alloc_small_dispatch " , [ L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; L [ K " param " ; K " i32 " ] ; ] ; " env " , " caml_alloc_small ...
let sanitize = String . map ( fun c -> match c with | ' A ' . . ' Z ' | ' a ' . . ' z ' | ' 0 ' . . ' 9 ' | ' _ ' | ' . ' -> c | _ -> ' ' ? )
let bigarray_to_string_list limit ba = let n = Array1 . dim ba in let l = ref [ ] in let p = ref 0 in while ! p < n do let q = min ( n - ! p ) limit in let by = Bytes . create q in for i = 0 to q - 1 do Bytes . set by i ba . { ! p + i } done ; l := Bytes . to_string by :: ! l...
let generate scode exe get_defname globals_table = let ( funcmapping , subfunctions ) = get_funcmapping scode in let letrec_name = Hashtbl . create 7 in Hashtbl . iter ( fun _ ( letrec_label , _ ) -> match letrec_label with | Main 0 -> Hashtbl . add letrec_name letrec_label " letrec_main...
type repr = | RValue | RInt | RIntUnclean | RIntVal | RNatInt | RInt32 | RInt64 | RFloat
type global_lookup = | Glb of int | Env of int
type store = | RealStack of int | RealAccu of { no_function : bool } | Const of int | Local of repr * string | Atom of int | TracedGlobal of global_lookup * int list * Wc_traceglobals . initvalue | Invalid
type global = Global of int
type label = Label of int | Loop of int
type stack_descriptor = { stack_uninit : int list ; stack_depth : int ; stack_save_accu : bool ; }
type unop = | Pnegint | Pboolnot | Poffsetint of int | Pisint | Pgetfield of int | Pgetfloatfield of int | Pvectlength | Pgetpubmet of int
type uneffect = | Poffsetref of int | Psetglobal of global
type binop = | Paddint | Psubint | Pmulint | Pdivint | Pmodint | Pandint | Porint | Pxorint | Plslint | Plsrint | Pasrint | Pintcomp of Lambda . integer_comparison | Puintcomp of Lambda . integer_comparison | Pgetvectitem | Pgetstringchar | Pgetbyteschar | Pgetmethod | Pgetdynmet
type bineffect = | Psetfield of int | Psetfloatfield of int
type terneffect = | Psetvectitem | Psetbyteschar
type winstruction = | Wcomment of string | Wblock of { label : label ; body : winstruction list } | Wcond of { cond : bool ref ; ontrue : winstruction ; onfalse : winstruction } | Wcopy of { src : store ; dest : store } | Walloc of { src : store ; dest : store ; descr : stack_d...
let repr_comparable_as_i32 r1 r2 = match r1 , r2 with | ( RValue | RIntVal ) , ( RValue | RIntVal ) -> true | ( RInt | RNatInt | RInt32 ) , ( RInt | RNatInt | RInt32 ) -> true | _ -> false
let repr_of_store = function | RealStack _ -> RValue | Const _ -> RInt | Local ( repr , _ ) -> repr | RealAccu _ -> RValue | Atom _ -> RValue | TracedGlobal _ -> RValue | Invalid -> assert false
let empty_descr = { stack_uninit = [ ] ; stack_depth = 0 ; stack_save_accu = false ; }
let string_of_store = function | RealStack pos -> sprintf " fp [ % d ] " pos | RealAccu _ -> " accu " | Const k -> sprintf " % d " k | Local ( repr , name ) -> name | Atom k -> sprintf " atom % d " k | TracedGlobal ( glb , path , _ ) -> let s_glb = match glb with | ...
let string_label = function | Label k -> sprintf " label % d " k | Loop k -> sprintf " loop % d " k
let extract_directly_callable_function st = match st with | TracedGlobal ( glb , path , FuncInEnv { func_offset ; env } ) -> let func = env . ( func_offset ) in ( match func with | Function { label } -> Some ( glb , path , label , env ) | _ -> None ) | _ -> None
let rec string_of_winstruction = function | Wcomment s -> s | Wblock arg -> ( match arg . label with | Label k -> sprintf " Wblock ( . . . , label % d ) " k | Loop k -> sprintf " Wblock ( loop % d , . . . ) " k ) | Wcond { cond ; ontrue ; onfalse } -> string_of...
let mk_table l = let tab = Hashtbl . create 7 in List . iter ( fun k -> Hashtbl . add tab k ( ) ) l ; tab
let prims_non_func_result = [ " caml_abs_float " ; " caml_acos_float " ; " caml_add_debug_info " ; " caml_add_float " ; " caml_alloc_dummy_float " ; " caml_array_append " ; " caml_array_blit " ; " caml_array_concat " ; " caml_array_fill " ; " caml_array_get_flo...
type executable = { dll_paths : string list ; dll_names : string list ; primitives : string array ; code : string ; data : ( char , int8_unsigned_elt , c_layout ) Array1 . t ; symbols : Symtable . global_map ; debug : ( int , string ) Hashtbl . t ; }
let read_bigstring f len = let ba = Array1 . create Char c_layout len in let by = Bytes . create 4096 in let n = ref len in let k = ref 0 in while ! n > 0 do let p = min ! n 4096 in really_input f by 0 p ; for i = 0 to p - 1 do ba . { ! k + i } <- Bytes . get by i done ; k := ! k ...
let input_stringlist f section = try let s = Bytesections . read_section_string f section in let l = String . split_on_char ' \ 000 ' s in List . filter ( fun elem -> elem <> " " ) l with | Not_found -> [ ]
let input_objarray_as_bigstring f section = try let _len = Bytesections . seek_section f section in let p1 = pos_in f in let _dummay = Marshal . from_channel f in let p2 = pos_in f in let _len = Bytesections . seek_section f section in read_bigstring f ( p2 - p1 ) with | Not_found -> Array1 . cr...
let input_symbols f section : Symtable . global_map = try let _len = Bytesections . seek_section f section in Marshal . from_channel f with | Not_found -> Symtable . empty_global_map
let input_debug f section = let ht = Hashtbl . create 7 in try let _len = Bytesections . seek_section f section in let n = input_binary_int f in for k = 1 to n do let ofs = input_binary_int f in let evl = input_value f in let _ = input_value f in List . iter ( fun ev -> Hashtbl . add ht I . ( ...
let read_executable name = let f = open_in name in Bytesections . read_toc f ; let dll_paths = input_stringlist f " DLPT " in let dll_names = input_stringlist f " DLLS " in let primitives = input_stringlist f " PRIM " |> Array . of_list in let code = Bytesections . read_section_string f " C...
type format = | C | L | LO | N | NN | LL
let instructions exec = [ O . opACC0 , [ ] , ( fun args -> [ I . Kacc 0 ] ) ; O . opACC1 , [ ] , ( fun args -> [ I . Kacc 1 ] ) ; O . opACC2 , [ ] , ( fun args -> [ I . Kacc 2 ] ) ; O . opACC3 , [ ] , ( fun args -> [ I . Kacc 3 ...
let map_label_in_instr f instr = match instr with | I . Klabel _ -> assert false | I . Kpush_retaddr lab -> I . Kpush_retaddr ( f lab ) | I . Kclosure ( lab , k ) -> I . Kclosure ( f lab , k ) | I . Kclosurerec ( labl , k ) -> I . Kclosurerec ( List . map f labl , k )...
let get_labels_in_instr instr = match instr with | I . Klabel _ -> assert false | I . Kpush_retaddr lab [ -> lab ] | I . Kclosure ( lab , k ) -> [ lab ] | I . Kclosurerec ( labl , k ) -> labl | I . Kbranch lab -> [ lab ] | I . Kbranchif lab -> [ lab ] | I . Kbran...
let decode exec = let instrs_l = instructions exec in let num = List . fold_left ( fun acc ( n , _ , _ ) -> max acc n ) ( - 1 ) instrs_l + 1 in let instrs = Array . make num ( [ ] , fun args -> failwith " bad instruction " ) in List . iter ( fun ( opcode , signature...
let defname_of_label exec map_label = let ht = Hashtbl . create 7 in Hashtbl . iter ( fun pos defname -> try let lab = map_label pos in try let ( old_pos , _ ) = Hashtbl . find ht lab in if pos < old_pos then Hashtbl . replace ht lab ( pos , defname ) with | Not_found -> Hashtbl . add ...
type sexp = | K of string | ID of string | N of number | S of string | C of string | BR | L of sexp list
let itable = Hashtbl . create 7
let indentation k = try Hashtbl . find itable k with | Not_found -> let s = String . make ( k / 8 ) ' \ t ' ^ String . make ( k mod 8 ) ' ' in Hashtbl . add itable k s ; s
let rec quote s = let b = Buffer . create 80 in String . iter ( fun c -> if c = ' " ' || c = ' ' \\ || Char . code c < 32 || Char . code c >= 127 then ( Buffer . add_char b ' ' ; \\ Buffer . add_char b Wc_util . hexdigits . ( Char . code c lsr 4 ) ; Buffer . ...
let rec string_of_sexp sexp = match sexp with | K x -> x | ID x -> " " $ ^ x | S x -> " " " \ ^ ( quote x ) ^ " " " \ | N x -> string_of_number x | C x -> sprintf " ( ; % s ; ) " x | BR -> " " | L l -> let l = List . filter ( fun e -> e <> BR ) l in " ...
let fits_onto_line width sexp = let rec single width sexp = match sexp with | K _ | ID _ | S _ | N _ | C _ | BR -> string_of_sexp sexp |> String . length | L l -> if width >= 2 then list ( width - 2 ) l + 2 else width + 1 and list width l = match l with | BR :: l ' -> list width l '...
let break l = let rec recurse acc l = match l with | BR :: l ' -> ( List . rev acc , l ' ) | x :: l ' -> recurse ( x :: acc ) l ' | [ ] -> ( [ ] , List . rev acc ) in recurse [ ] l
let rec print_indented f indent width sexp = match sexp with | K _ | ID _ | S _ | N _ | C _ | BR -> fprintf f " % s % s " ( indentation indent ) ( string_of_sexp sexp ) | L l -> if width > indent && fits_onto_line ( width - indent ) sexp then fprintf f " % s % s " ( indentati...
let rec ascii s = let b = Buffer . create 80 in Buffer . add_string b " \ t . ascii " " ; \ String . iteri ( fun i c -> if i > 0 && i mod 32 = 0 then ( Buffer . add_string b " " \\ n \ t . ascii " " ; \ ) ; if c = ' " ' || c = ' ' \\ || Char . code c...
let extract_func_type l = let rec scan params results l = match l with | ( L [ K " param " ; K ty ] ) :: l ' -> scan ( ty :: params ) results l ' | ( L [ K " result " ; K ty ] ) :: l ' -> scan params ( ty :: results ) l ' | other -> let llvm_type = sprintf " ( ...
let abbrev_empty_type s = if s = " ( ) -> ( ) " then " " else s
let llvm_type_of_func_type typeuse = let ( llvm_type , rest ) = extract_func_type typeuse in if rest <> [ ] then failwith ( " llvm_type_of_func_type : " ^ string_of_sexp ( L rest ) ) ; llvm_type
let extract_label l = match l with | ( ID label ) :: rest -> ( label , rest ) | _ -> ( " < empty " , > l )
let extract_load_store_params l = let rec recurse offset align l = match l with | ( K tok ) :: rest -> ( try let offset = Scanf . sscanf tok " offset =% i " ( fun x -> x ) in recurse offset align rest with End_of_file | Scanf . Scan_failure _ -> ( try let align = Scanf . sscanf tok " ...
let find_label name l = let rec recurse i l = match l with | x :: l ' -> if name = x then i else recurse ( i + 1 ) l ' | [ ] -> raise Not_found in recurse 0 l
let itable = Hashtbl . create 7
let indentation k = try Hashtbl . find itable k with | Not_found -> let s = String . make ( k / 8 ) ' \ t ' ^ String . make ( k mod 8 ) ' ' in Hashtbl . add itable k s ; s
let write_func_body f func_name locals_table sexpl = let rec next labels depth sexpl = let bad ( ) = failwith ( sprintf " bad code - function % s - % s " func_name ( string_of_sexp ( L sexpl ) ) ) in let indent = indentation ( 4 * depth ) in match sexpl with | L [ K ( " local . ...
let rec remove_stuff l = let rec f = function | BR -> None | C _ -> None | L l -> Some ( L ( remove_stuff l ) ) | other -> Some other in List . filter_map f l
let write_file f filename sexpl = let import sym mod_name obj_name = fprintf f " \ t . import_module % s , % s \ n " sym mod_name ; fprintf f " \ t . import_name % s , % s \ n " sym obj_name in let export sym obj_name = fprintf f " \ t . export_name % s , % s \ n " sym obj_name ...
type initvalue = | Unknown | Function of { label : int } | FuncInEnv of { func_offset : int ; env : initvalue array } | Block of initvalue array
let is_function = function | Function _ -> true | _ -> false
type shape = { stack : initvalue list ; length : int ; accu : initvalue }
let empty_shape = { stack = [ ] ; length = 0 ; accu = Unknown }
let print_initvalue prefix initvalue = let rec recurse prefix initvalue = match initvalue with | Unknown -> ( ) | Function fn -> printf " % s = letrec_ % d \ n " prefix fn . label | FuncInEnv fenv -> Array . iteri ( fun i iv -> recurse ( sprintf " % s . env [ % d ] ( % d ) " ...
let rec merge_initvalues v1 v2 = match v1 , v2 with | Function fn1 , Function fn2 when fn1 . label = fn2 . label -> v1 | FuncInEnv fenv1 , FuncInEnv fenv2 when fenv1 . func_offset = fenv2 . func_offset && Array . length fenv1 . env = Array . length fenv2 . env -> let env = merge_initvalu...
let merge_stacks s1 s2 = assert ( s1 . length = s2 . length ) ; { stack = List . map2 merge_initvalues s1 . stack s2 . stack ; length = s1 . length ; accu = merge_initvalues s1 . accu s2 . accu ; }
let rec delete n l = if n <= 0 then l else match l with | x :: l ' -> delete ( n - 1 ) l ' | [ ] -> [ ]
let global_offset ident = assert ( Ident . global ident ) ; let name = Ident . name ident in int_of_string name
let trace_globals_of_fblock globals_table fblock = let shape_table = Hashtbl . create 7 in let update_shape_table stack labels = List . iter ( fun label -> try let lstack = Hashtbl . find shape_table label in if lstack . length <> stack . length then ( eprintf " [ DEBUG ] Bad function : % d...
let trace_globals scode = let globals_table = Hashtbl . create 7 in IMap . iter ( fun func_label fblock -> if fblock . scope . cfg_main then trace_globals_of_fblock globals_table fblock ) scode . functions ; globals_table