text stringlengths 0 601k |
|---|
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 ] ) ; O . opACC4 , [ ] , ( fun args -> [ I . Kacc 4 ] ) ; O . opACC5 , [ ] , ( fun args -> [... |
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 ) | I . Kbranch lab -> I . Kbranch ( f lab ) | I . Kb... |
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 . Kbranchifnot lab -> [ lab ] | I . Kstrictbranchif _ -> assert ... |
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 , decoder ) -> instrs . ( opcode ) <- ( signature , d... |
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 ht lab ( pos , defname ) with | No... |
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 . add_char b Wc_util . hexdigits . ( Char . code c land 15 ) ; ... |
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 " ( " ^ string_of_sexp_list l ^ " ) " List . map string_of_sexp l |> S... |
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 ' | x :: l ' -> let len = single width... |
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 " ( indentation indent ) ( string_of_sexp sexp ) else let... |
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 < 32 || Char . code c >= 127 then ( Buffer . add_char b ' ' ; \\ Buff... |
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 " ( % s ) -> ( % s ) " ( String . concat " , " ( List . rev par... |
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 " align =% i " ( fun x -> x ) in recu... |
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 . get " " | local . set " " | local . tee "... |
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 in let global glb_name w_type = let glb_t... |
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 ) " prefix i fenv . func_offset ) iv ) fenv . en... |
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_initvalue_arrays fenv1 . env fenv2 . env... |
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 \ n " fblock . scope . cfg_f... |
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 |
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 { label } -> Hashtbl . replace glbfun... |
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 | Ksetglobal _ | Kgetfield _ | Kgetfloa... |
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 ' ( k + 1 ) else mdepth in recurse 0 0 0 |
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 | Some l -> sprintf " break =% d " l | None -> ... |
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 . scope . cfg_func_label ; eprin... |
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 . t ; mutable environment : Wc_traceglobals . initvalue... |
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 ) ) | _ -> ( ) ) state . camlstack ; let uninit = ref... |
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 = ISet . remove cpos state . realaccu ; } | ( Co... |
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 ; camldepth = cd + 1 ; realaccu = ISet . add cpos... |
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 = ( - state . camldepth + i ) in if ISet .... |
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 = real_accu } in ( state , instrs_flush @ [ ... |
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 match camlstack with | hd :: tl -> hd... |
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 acc ' = ISet . add q acc in ( q + 1... |
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 _ -> assert ( ISet . mem pos state . realaccu ... |
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 . rev rev_acc ) |
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 + i -> [ - cd + i ] | _ -> [ - cd + i ... |
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 . mapi ( fun i st -> if st = state . acc... |
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 instrs_op = [ Wunar... |
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 state = { state with ac... |
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 = op_code ; src1 ; src2 ; src3 } ] in ... |
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 _ | Local _ | Invalid -> true | _ -> false ) ; asser... |
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 , instrs2 ) = straighten_stack lpa... |
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 { state with accu = List . nth state . camlstack s... |
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 . replace indegree lab ( n ... |
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 " Kassign ( % d ) " k | Kpush_retaddr label -> 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 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.