text
stringlengths
12
786k
module Key = struct type t = { tag : Dwarf_tag . t ; has_children : Child_determination . t ; attribute_specs : Dwarf_attributes . Attribute_specification . Sealed . Set . t } include Identifiable . Make ( struct type nonrec t = t let compare { tag = tag1 ; has_children = has_children1 ; attribute_specs = attribute_specs1 } { tag = tag2 ; has_children = has_children2 ; attribute_specs = attribute_specs2 } = let c = Dwarf_tag . compare tag1 tag2 in if c <> 0 then c else let c = Child_determination . compare has_children1 has_children2 in if c <> 0 then c else Dwarf_attributes . Attribute_specification . Sealed . Set . compare attribute_specs1 attribute_specs2 let equal t1 t2 = compare t1 t2 = 0 let hash _ = Misc . fatal_error " Not yet implemented " let output _ _ = Misc . fatal_error " Not yet implemented " let print _ _ = Misc . fatal_error " Not yet implemented " end ) end
type t = Abbreviations_table_entry . t Key . Map . t
let create ( ) = Key . Map . empty
let add t entry = let tag = Abbreviations_table_entry . tag entry in let has_children = Abbreviations_table_entry . has_children entry in let attribute_specs = Abbreviations_table_entry . attribute_specs entry in let key : Key . t = { tag ; has_children ; attribute_specs } in Key . Map . add key entry t
let find t ~ tag ~ has_children ~ attribute_specs = let key : Key . t = { tag ; has_children ; attribute_specs } in match Key . Map . find key t with | exception Not_found -> None | entry -> Some ( Abbreviations_table_entry . abbreviation_code entry )
let size t = let ( + ) = Dwarf_int . add in Dwarf_value . size ( Dwarf_value . uleb128 Uint64 . zero ) + Key . Map . fold ( fun _key entry size -> size + Abbreviations_table_entry . size entry ) t ( Dwarf_int . zero ( ) )
let emit ~ asm_directives t = let module Asm_directives = ( val asm_directives : Asm_directives . S ) in Key . Map . iter ( fun _key entry -> Asm_directives . new_line ( ) ; Abbreviations_table_entry . emit ~ asm_directives entry ) t ; Dwarf_value . emit ~ asm_directives ( Dwarf_value . uleb128 ~ comment " : End of abbrevs for compilation unit " Uint64 . zero )
type t = { abbreviation_code : Abbreviation_code . t ; tag : Dwarf_tag . t ; has_children : Child_determination . t ; attribute_specs : Dwarf_attributes . Attribute_specification . Sealed . Set . t }
let create ~ abbreviation_code ~ tag ~ has_children ~ attribute_specs = { abbreviation_code ; tag ; has_children ; attribute_specs }
let size t = let ( + ) = Dwarf_int . add in Abbreviation_code . size t . abbreviation_code + Dwarf_tag . size t . tag + Child_determination . size t . has_children + AS . Set . fold ( fun attr_spec size -> Dwarf_int . add size ( AS . size attr_spec ) ) t . attribute_specs ( Dwarf_int . zero ( ) ) + Dwarf_value . size ( Dwarf_value . uleb128 Uint64 . zero ) + Dwarf_value . size ( Dwarf_value . uleb128 Uint64 . zero )
let emit ~ asm_directives t = Abbreviation_code . emit ~ asm_directives t . abbreviation_code ; Dwarf_tag . emit ~ asm_directives t . tag ; Child_determination . emit ~ asm_directives t . has_children ; AS . Set . iter ( fun spec -> AS . emit ~ asm_directives spec ) t . attribute_specs ; Dwarf_value . emit ~ asm_directives ( Dwarf_value . uleb128 ~ comment " : terminator word 1 " Uint64 . zero ) ; Dwarf_value . emit ~ asm_directives ( Dwarf_value . uleb128 ~ comment " : terminator word 2 " Uint64 . zero )
let tag t = t . tag
let has_children t = t . has_children
let attribute_specs t = t . attribute_specs
let abbreviation_code t = t . abbreviation_code
let rec ack ( x , y ) = if x = 0 then y + 1 else if y = 0 then ack ( x - 1 , 1 ) else ack ( x - 1 , ack ( x , y - 1 ) )
let rec ack_cps ( x , y , cnt ) = if x = 0 then cnt ( y + 1 ) else if y = 0 then ack_cps ( x - 1 , 1 , cnt ) else ack_cps ( x , y - 1 , fun a -> ack_cps ( x - 1 , a , cnt ) )
let ack_2 ( x , y ) = ack_cps ( x , y , fun x -> x )
let rec ack_cps_dfc ( x , y , cnt ) = if x = 0 then apply_cnt ( cnt , y + 1 ) else if y = 0 then ack_cps_dfc ( x - 1 , 1 , cnt ) else ack_cps_dfc ( x , y - 1 , x :: cnt ) | ( [ ] , x ) -> x | ( x :: cnt , a ) -> ack_cps_dfc ( x - 1 , a , cnt )
let ack_3 ( x , y ) = ack_cps_dfc ( x , y , [ ] )
type state = ACK of int * int * ( int list ) | APP of ( int list ) * int
let rec string_of_int_list_aux = function | [ ] -> " " | [ t ] -> ( string_of_int t ) | t :: rest -> ( string_of_int t ) ^ " , " ^ ( string_of_int_list_aux rest )
let string_of_int_list l = " [ " ^ ( string_of_int_list_aux l ) ^ " ] "
let print_state n = function | APP ( cnt , m ) -> print_string ( ( string_of_int n ) ^ " " | ACK ( x , y , cnt ) -> print_string ( ( string_of_int n ) ^ " "
let step = function | ACK ( 0 , y , cnt ) -> APP ( cnt , y + 1 ) | ACK ( x , 0 , cnt ) -> ACK ( x - 1 , 1 , cnt ) | ACK ( x , y , cnt ) -> ACK ( x , y - 1 , x :: cnt ) | APP ( x :: cnt , a ) -> ACK ( x - 1 , a , cnt ) | s -> s
let rec driver n state = let _ = print_state n state in match state with | APP ( [ ] , v ) -> v | s -> driver ( n + 1 ) ( step s )
let ack_4 ( x , y ) = driver 0 ( ACK ( x , y , [ ] ) )
type code = out_channel -> Environments . t -> Result . t * Environments . t
type t = { name : string ; body : code ; mutable hook : code option }
let name a = a . name
let action_name = Variables . make ( " action_name " , " Name of the current action " )
let make n c = { name = n ; body = c ; hook = None }
let update action code = { action with body = code }
let compare a1 a2 = String . compare a1 . name a2 . name
let ( actions : ( string , t ) Hashtbl . t ) = Hashtbl . create 10
let register action = Hashtbl . add actions action . name action
let get_registered_actions ( ) = let f _name action acc = action :: acc in let unsorted_actions = Hashtbl . fold f actions [ ] in List . sort compare unsorted_actions
let lookup name = try Some ( Hashtbl . find actions name ) with Not_found -> None
let set_hook name hook = let action = ( Hashtbl . find actions name ) in action . hook <- Some hook
let clear_hook name = let action = ( Hashtbl . find actions name ) in action . hook <- None
let clear_all_hooks ( ) = let f _name action = action . hook <- None in Hashtbl . iter f actions
let run log env action = let code = match action . hook with | None -> action . body | Some code -> code in let env = Environments . add action_name action . name env in code log env
module ActionSet = Set . Make ( struct type nonrec t = t let compare = compare end )
let _ = Variables . register_variable action_name
let skip_with_reason reason = let code _log env = let result = Result . skip_with_reason reason in ( result , env ) in Actions . make " skip " code
let pass_or_skip test pass_reason skip_reason _log env = let open Result in let result = if test then pass_with_reason pass_reason else skip_with_reason skip_reason in ( result , env )
let mkreason what commandline exitcode = Printf . sprintf " % s : command \ n % s \ nfailed with exit code % d " what commandline exitcode
let testfile env = match Environments . lookup Builtin_variables . test_file env with | None -> assert false | Some t -> t
let test_source_directory env = Environments . safe_lookup Builtin_variables . test_source_directory env
let test_build_directory env = Environments . safe_lookup Builtin_variables . test_build_directory env
let test_build_directory_prefix env = Environments . safe_lookup Builtin_variables . test_build_directory_prefix env
let words_of_variable env variable = String . words ( Environments . safe_lookup variable env )
let exit_status_of_variable env variable = try int_of_string ( Environments . safe_lookup variable env ) with _ -> 0
let files env = words_of_variable env Builtin_variables . files
let setup_symlinks test_source_directory build_directory files = let symlink filename = let src = Filename . concat test_source_directory filename in let dst = Filename . concat build_directory filename in let ( ) = if Sys . file_exists dst then if Sys . win32 && Sys . is_directory dst then Sys . rmdir dst else Sys . remove dst in Unix . symlink src dst in let copy filename = let src = Filename . concat test_source_directory filename in let dst = Filename . concat build_directory filename in Sys . copy_file src dst in let f = if Unix . has_symlink ( ) then symlink else copy in Sys . make_directory build_directory ; List . iter f files
let setup_build_env add_testfile additional_files ( _log : out_channel ) env = let build_dir = ( test_build_directory env ) in let some_files = additional_files @ ( files env ) in let files = if add_testfile then ( testfile env ) :: some_files else some_files in setup_symlinks ( test_source_directory env ) build_dir files ; Sys . chdir build_dir ; ( Result . pass , env )
let setup_simple_build_env add_testfile additional_files log env = let build_env = Environments . add Builtin_variables . test_build_directory ( test_build_directory_prefix env ) env in setup_build_env add_testfile additional_files log build_env
let run_cmd ( ? environment [ ] ) =|| ( ? stdin_variable = Builtin_variables . stdin ) ( ? stdout_variable = Builtin_variables . stdout ) ( ? stderr_variable = Builtin_variables . stderr ) ( ? append = false ) ( ? timeout = 0 ) log env original_cmd = let log_redirection std filename = if filename " " <> then begin Printf . fprintf log " Redirecting % s to % s \ n " %! std filename end in let cmd = if ( Environments . lookup_as_bool Strace . strace env ) = Some true then begin let action_name = Environments . safe_lookup Actions . action_name env in let test_build_directory = test_build_directory env in let strace_logfile_name = Strace . get_logfile_name action_name in let strace_logfile = Filename . make_path [ test_build_directory ; strace_logfile_name ] in let strace_flags = Environments . safe_lookup Strace . strace_flags env in let strace_cmd = [ " strace " ; " - f " ; " - o " ; strace_logfile ; strace_flags ] in strace_cmd @ original_cmd end else original_cmd in let lst = List . concat ( List . map String . words cmd ) in let quoted_lst = if Sys . win32 then List . map Filename . maybe_quote lst else lst in let cmd ' = String . concat " " quoted_lst in Printf . fprintf log " Commandline : % s \ n " cmd ' ; let progname = List . hd quoted_lst in let arguments = Array . of_list quoted_lst in let stdin_filename = Environments . safe_lookup stdin_variable env in let stdout_filename = Environments . safe_lookup stdout_variable env in let stderr_filename = Environments . safe_lookup stderr_variable env in log_redirection " stdin " stdin_filename ; log_redirection " stdout " stdout_filename ; log_redirection " stderr " stderr_filename ; let systemenv = Array . append environment ( Environments . to_system_env env ) in let n = Run_command . run { Run_command . progname = progname ; Run_command . argv = arguments ; Run_command . envp = systemenv ; Run_command . stdin_filename = stdin_filename ; Run_command . stdout_filename = stdout_filename ; Run_command . stderr_filename = stderr_filename ; Run_command . append = append ; Run_command . timeout = timeout ; Run_command . log = log } in let dump_file s fn = if not ( Sys . file_is_empty fn ) then begin Printf . fprintf log " ### begin % s ###\ n " s ; Sys . dump_file log fn ; Printf . fprintf log " ### end % s ###\ n " s end in dump_file " stdout " stdout_filename ; if stdout_filename <> stderr_filename then dump_file " stderr " stderr_filename ; n
let run ( log_message : string ) ( redirect_output : bool ) ( can_skip : bool ) ( prog_variable : Variables . t ) ( args_variable : Variables . t option ) ( log : out_channel ) ( env : Environments . t ) = match Environments . lookup prog_variable env with | None -> let msg = Printf . sprintf " % s : variable % s is undefined " log_message ( Variables . name_of_variable prog_variable ) in ( Result . fail_with_reason msg , env ) | Some program -> let arguments = match args_variable with | None -> " " | Some variable -> Environments . safe_lookup variable env in let commandline = [ program ; arguments ] in let what = log_message ^ " " ^ program ^ " " ^ begin if arguments " " = then " without any argument " else " with arguments " ^ arguments end in let env = if redirect_output then begin let output = Environments . safe_lookup Builtin_variables . output env in let env = Environments . add_if_undefined Builtin_variables . stdout output env in Environments . add_if_undefined Builtin_variables . stderr output env end else env in let expected_exit_status = exit_status_of_variable env Builtin_variables . exit_status in let exit_status = run_cmd log env commandline in if exit_status = expected_exit_status then ( Result . pass , env ) else begin let reason = mkreason what ( String . concat " " commandline ) exit_status in if exit_status = 125 && can_skip then ( Result . skip_with_reason reason , env ) else ( Result . fail_with_reason reason , env ) end
let run_program = run " Running program " true false Builtin_variables . program ( Some Builtin_variables . arguments )
let run_script log env = let response_file = Filename . temp_file " ocamltest " - " . response " in Printf . fprintf log " Script should write its response to % s \ n " %! response_file ; let scriptenv = Environments . add Builtin_variables . ocamltest_response response_file env in let ( result , newenv ) = run " Running script " true true Builtin_variables . script None log scriptenv in let final_value = if Result . is_pass result then begin match Modifier_parser . modifiers_of_file response_file with | modifiers -> let modified_env = Environments . apply_modifiers newenv modifiers in ( result , modified_env ) | exception Failure reason -> ( Result . fail_with_reason reason , newenv ) | exception Variables . No_such_variable name -> let reason = Printf . sprintf " error in script response : unknown variable % s " name in ( Result . fail_with_reason reason , newenv ) end else begin let reason = String . trim ( Sys . string_of_file response_file ) in let newresult = { result with Result . reason = Some reason } in ( newresult , newenv ) end in Sys . force_remove response_file ; final_value
let run_hook hook_name log input_env = Printf . fprintf log " Entering run_hook for hook % s \ n " %! hook_name ; let response_file = Filename . temp_file " ocamltest " - " . response " in Printf . fprintf log " Hook should write its response to % s \ n " %! response_file ; let hookenv = Environments . add Builtin_variables . ocamltest_response response_file input_env in let systemenv = Environments . to_system_env hookenv in let open Run_command in let settings = { progname = " sh " ; argv = [ " | sh " ; Filename . maybe_quote hook_name ] ; | envp = systemenv ; stdin_filename = " " ; stdout_filename = " " ; stderr_filename = " " ; append = false ; timeout = 0 ; log = log ; } in let exit_status = run settings in let final_value = match exit_status with | 0 -> begin match Modifier_parser . modifiers_of_file response_file with | modifiers -> let modified_env = Environments . apply_modifiers hookenv modifiers in ( Result . pass , modified_env ) | exception Failure reason -> ( Result . fail_with_reason reason , hookenv ) | exception Variables . No_such_variable name -> let reason = Printf . sprintf " error in script response : unknown variable % s " name in ( Result . fail_with_reason reason , hookenv ) end | _ -> Printf . fprintf log " Hook returned % d " exit_status ; let reason = String . trim ( Sys . string_of_file response_file ) in if exit_status = 125 then ( Result . skip_with_reason reason , hookenv ) else ( Result . fail_with_reason reason , hookenv ) in Sys . force_remove response_file ; final_value
let check_output kind_of_output output_variable reference_variable log env = let to_int = function None -> 0 | Some s -> int_of_string s in let skip_lines = to_int ( Environments . lookup Builtin_variables . skip_header_lines env ) in let skip_bytes = to_int ( Environments . lookup Builtin_variables . skip_header_bytes env ) in let reference_filename = Environments . safe_lookup reference_variable env in let output_filename = Environments . safe_lookup output_variable env in Printf . fprintf log " Comparing % s output % s to reference % s \ n " %! kind_of_output output_filename reference_filename ; let files = { Filecompare . filetype = Filecompare . Text ; Filecompare . reference_filename = reference_filename ; Filecompare . output_filename = output_filename } in let ignore_header_conf = { Filecompare . lines = skip_lines ; Filecompare . bytes = skip_bytes ; } in let tool = Filecompare . make_cmp_tool ~ ignore : ignore_header_conf in match Filecompare . check_file ~ tool files with | Filecompare . Same -> ( Result . pass , env ) | Filecompare . Different -> let diff = Filecompare . diff files in let diffstr = match diff with | Ok difference -> difference | Error diff_file -> ( " See " ^ diff_file ) in let reason = Printf . sprintf " % s output % s differs from reference % s : \ n % s \ n " kind_of_output output_filename reference_filename diffstr in if Environments . lookup_as_bool Builtin_variables . promote env = Some true then begin Printf . fprintf log " Promoting % s output % s to reference % s \ n " %! kind_of_output output_filename reference_filename ; Filecompare . promote files ignore_header_conf ; end ; ( Result . fail_with_reason reason , env ) | Filecompare . Unexpected_output -> let banner = String . make 40 ' ' = in let unexpected_output = Sys . string_of_file output_filename in let unexpected_output_with_banners = Printf . sprintf " % s \ n % s % s \ n " banner unexpected_output banner in let reason = Printf . sprintf " The file % s was expected to be empty because there is no \ reference file % s but it is not :\ n % s \ n " output_filename reference_filename unexpected_output_with_banners in ( Result . fail_with_reason reason , env ) | Filecompare . Error ( commandline , exitcode ) -> let reason = Printf . sprintf " The command % s failed with status % d " commandline exitcode in ( Result . fail_with_reason reason , env )
let main ( ) = let top = opentk ( ) in let en1 = Entry . create top [ TextWidth 6 ; Relief Sunken ] in let lab1 = Label . create top [ Text " plus " ] in let en2 = Entry . create top [ TextWidth 6 ; Relief Sunken ] in let lab2 = Label . create top [ Text " " ] = in let result_display = Label . create top [ ] in let n1 = ref 0 and n2 = ref 0 in let refresh ( ) = Label . configure result_display [ Text ( string_of_int ( ! n1 + ! n2 ) ) ] in let get_and_refresh ( w , r ) = fun _ _ -> try r := int_of_string ( Entry . get w ) ; refresh ( ) with Failure " int_of_string " -> Label . configure result_display [ Text " error " ] in Entry . configure en1 [ XScrollCommand ( get_and_refresh ( en1 , n1 ) ) ] ; Entry . configure en2 [ XScrollCommand ( get_and_refresh ( en2 , n2 ) ) ] ; pack [ en1 ; lab1 ; en2 ; lab2 ; result_display ] [ ] ; Wm . minsize_set top 1 1 ; mainLoop ( ) ; ;
let _ = Printexc . catch main ( ) ; ;
let norec = ref false
let input_file file = let ic = try open_in file with _ -> failwith ( " input_file : " ^ file ) in let b = Buffer . create 1024 in let buf = String . create 1024 and len = ref 0 in while len := input ic buf 0 1024 ; ! len > 0 do Buffer . add_substring b buf 0 ! len done ; close_in ic ; Buffer . contents b
module SMap = struct include Map . Make ( struct type t = string let compare = compare end ) let rec removes l m = match l with [ ] -> m | k :: l -> let m = try remove k m with Not_found -> m in removes l m end
let rec labels_of_sty sty = match sty . ptyp_desc with Ptyp_arrow ( lab , _ , rem ) -> lab :: labels_of_sty rem | Ptyp_alias ( rem , _ ) -> labels_of_sty rem | _ -> [ ]
let rec labels_of_cty cty = match cty . pcty_desc with Pcty_fun ( lab , _ , rem ) -> let ( labs , meths ) = labels_of_cty rem in ( lab :: labs , meths ) | Pcty_signature ( _ , fields ) -> ( [ ] , List . fold_left fields ~ init [ ] : ~ f : begin fun meths -> function Pctf_meth ( s , _ , sty , _ ) -> ( s , labels_of_sty sty ) :: meths | _ -> meths end ) | _ -> ( [ ] , [ ] )
let rec pattern_vars pat = match pat . ppat_desc with Ppat_var s -> [ s ] | Ppat_alias ( pat , s ) -> s :: pattern_vars pat | Ppat_tuple l | Ppat_array l -> List . concat ( List . map pattern_vars l ) | Ppat_construct ( _ , Some pat , _ ) | Ppat_variant ( _ , Some pat ) | Ppat_constraint ( pat , _ ) -> pattern_vars pat | Ppat_record ( l , _ ) -> List . concat ( List . map l ~ f ( : fun ( _ , p ) -> pattern_vars p ) ) | Ppat_or ( pat1 , pat2 ) -> pattern_vars pat1 @ pattern_vars pat2 | Ppat_lazy pat -> pattern_vars pat | Ppat_any | Ppat_constant _ | Ppat_construct _ | Ppat_variant _ | Ppat_type _ -> [ ]
let pattern_name pat = match pat . ppat_desc with Ppat_var s -> Some s | Ppat_constraint ( { ppat_desc = Ppat_var s } , _ ) -> Some s | _ -> None
let insertions = ref [ ]
let add_insertion pos s = insertions := ( pos , s ) :: ! insertions
let sort_insertions ( ) = List . sort ! insertions ~ cmp ( : fun ( pos1 , _ ) ( pos2 , _ ) -> pos1 - pos2 )
let is_space = function ' ' ' |\ t ' ' |\ n ' ' |\ r ' -> true | _ -> false
let is_alphanum = function ' A ' . . ' Z ' ' | a ' . . ' z ' ' | _ ' ' |\ 192 ' . . ' \ 214 ' ' |\ 216 ' . . ' \ 246 ' | ' \ 248 ' . . ' \ 255 ' ' ' ' ' |\| 0 ' . . ' 9 ' -> true | _ -> false
let rec insertion_point pos ~ text = let pos ' = ref ( pos - 1 ) in while is_space text . [ ! pos ' ] do decr pos ' done ; if text . [ ! pos ' ] = ' ( ' then insertion_point ! pos ' ~ text else if ! pos ' >= 5 && String . sub text ~ pos ( :! pos ' - 4 ) ~ len : 5 = " begin " && not ( is_alphanum text . [ ! pos ' - 5 ] ) then insertion_point ( ! pos ' - 4 ) ~ text else pos
let rec insertion_point2 pos ~ text = let pos ' = ref ( pos - 1 ) in while is_space text . [ ! pos ' ] do decr pos ' done ; if text . [ ! pos ' ] = ' ( ' then insertion_point2 ! pos ' ~ text else if ! pos ' >= 5 && String . sub text ~ pos ( :! pos ' - 4 ) ~ len : 5 = " begin " && not ( is_alphanum text . [ ! pos ' - 5 ] ) then insertion_point2 ( ! pos ' - 4 ) ~ text else if text . [ ! pos ' ] = ' ' = then Some ! pos ' else if ! pos ' >= 1 && text . [ ! pos ' - 1 ] = ' ' - && text . [ ! pos ' ] = ' ' > then Some ( ! pos ' - 1 ) else None
let rec insert_labels ~ labels ~ text expr = match labels , expr . pexp_desc with l :: labels , Pexp_function ( l ' , _ , [ pat , rem ] ) -> if l <> " " && l . [ 0 ] <> ' ' ? && l ' = " " then begin let start_c = pat . ppat_loc . Location . loc_start . Lexing . pos_cnum in let pos = insertion_point start_c ~ text in match pattern_name pat with | Some name when l = name -> add_insertion pos " " ~ | _ -> add_insertion pos ( " " ~ ^ l ^ " " ) : end ; insert_labels ~ labels ~ text rem | l :: labels , Pexp_function ( l ' , _ , lst ) -> let pos = expr . pexp_loc . Location . loc_start . Lexing . pos_cnum in if l <> " " && l . [ 0 ] <> ' ' ? && l ' = " " && String . sub text ~ pos ~ len : 8 = " function " then begin String . blit ~ src " : match th " ~ src_pos : 0 ~ dst : text ~ dst_pos : pos ~ len : 8 ; add_insertion ( pos + 6 ) ( l ^ " wi " ) ; match insertion_point2 pos ~ text with Some pos ' -> add_insertion pos ' ( " " ~ ^ l ^ " " ) | None -> add_insertion pos ( " fun " ~ ^ l ^ " -> " ) end ; List . iter lst ~ f ( : fun ( p , e ) -> insert_labels ~ labels ~ text e ) | _ , Pexp_match ( _ , lst ) -> List . iter lst ~ f ( : fun ( p , e ) -> insert_labels ~ labels ~ text e ) | _ , Pexp_try ( expr , lst ) -> insert_labels ~ labels ~ text expr ; List . iter lst ~ f ( : fun ( p , e ) -> insert_labels ~ labels ~ text e ) | _ , ( Pexp_let ( _ , _ , e ) | Pexp_sequence ( _ , e ) | Pexp_when ( _ , e ) | Pexp_constraint ( e , _ , _ ) | Pexp_letmodule ( _ , _ , e ) | Pexp_ifthenelse ( _ , e , None ) ) -> insert_labels ~ labels ~ text e | _ , Pexp_ifthenelse ( _ , e1 , Some e2 ) -> insert_labels ~ labels ~ text e1 ; insert_labels ~ labels ~ text e2 | _ -> ( )
let rec insert_labels_class ~ labels ~ text expr = match labels , expr . pcl_desc with l :: labels , Pcl_fun ( l ' , _ , pat , rem ) -> if l <> " " && l . [ 0 ] <> ' ' ? && l ' = " " then begin let start_c = pat . ppat_loc . Location . loc_start . Lexing . pos_cnum in let pos = insertion_point start_c ~ text in match pattern_name pat with | Some name when l = name -> add_insertion pos " " ~ | _ -> add_insertion pos ( " " ~ ^ l ^ " " ) : end ; insert_labels_class ~ labels ~ text rem | labels , ( Pcl_constraint ( expr , _ ) | Pcl_let ( _ , _ , expr ) ) -> insert_labels_class ~ labels ~ text expr | _ -> ( )
let rec insert_labels_type ~ labels ~ text ty = match labels , ty . ptyp_desc with l :: labels , Ptyp_arrow ( l ' , _ , rem ) -> if l <> " " && l . [ 0 ] <> ' ' ? && l ' = " " then begin let start_c = ty . ptyp_loc . Location . loc_start . Lexing . pos_cnum in let pos = insertion_point start_c ~ text in add_insertion pos ( l ^ " " ) : end ; insert_labels_type ~ labels ~ text rem | _ -> ( )
let rec insert_labels_app ~ labels ~ text args = match labels , args with l :: labels , ( l ' , arg ) :: args -> if l <> " " && l . [ 0 ] <> ' ' ? && l ' = " " then begin let pos0 = arg . pexp_loc . Location . loc_start . Lexing . pos_cnum in let pos = insertion_point pos0 ~ text in match arg . pexp_desc with | Pexp_ident ( Longident . Lident name ) when l = name && pos = pos0 -> add_insertion pos " " ~ | _ -> add_insertion pos ( " " ~ ^ l ^ " " ) : end ; insert_labels_app ~ labels ~ text args | _ -> ( )
let insert_labels_app ~ labels ~ text args = let labels , opt_labels = List . partition labels ~ f ( : fun l -> l = " " || l . [ 0 ] <> ' ' ) ? in let nopt_labels = List . map opt_labels ~ f ( : fun l -> String . sub l ~ pos : 1 ~ len ( : String . length l - 1 ) ) in if List . exists labels ~ f ( : List . mem ~ set : nopt_labels ) then ( ) else let aopt_labels = opt_labels @ nopt_labels in let args , lab_args = List . partition args ~ f ( : fun ( l , _ ) -> l = " " ) in if List . for_all lab_args ~ f ( : fun ( l , _ ) -> List . mem l ~ set : aopt_labels ) then insert_labels_app ~ labels ~ text args
let rec add_labels_expr ~ text ~ values ~ classes expr = let add_labels_rec ( ? values = values ) expr = add_labels_expr ~ text ~ values ~ classes expr in match expr . pexp_desc with Pexp_apply ( { pexp_desc = Pexp_ident ( Longident . Lident s ) } , args ) -> begin try let labels = SMap . find s values in insert_labels_app ~ labels ~ text args with Not_found -> ( ) end ; List . iter args ~ f ( : fun ( _ , e ) -> add_labels_rec e ) | Pexp_apply ( { pexp_desc = Pexp_send ( { pexp_desc = Pexp_ident ( Longident . Lident s ) } , meth ) } , args ) -> begin try if SMap . find s values = [ " < object " ] > then let labels = SMap . find ( s ^ " " # ^ meth ) values in insert_labels_app ~ labels ~ text args with Not_found -> ( ) end | Pexp_apply ( { pexp_desc = Pexp_new ( Longident . Lident s ) } , args ) -> begin try let labels = SMap . find s classes in insert_labels_app ~ labels ~ text args with Not_found -> ( ) end | Pexp_let ( recp , lst , expr ) -> let vars = List . concat ( List . map lst ~ f ( : fun ( p , _ ) -> pattern_vars p ) ) in let vals = SMap . removes vars values in List . iter lst ~ f : begin fun ( _ , e ) -> add_labels_rec e ~ values ( : if recp = Recursive then vals else values ) end ; add_labels_rec expr ~ values : vals | Pexp_function ( _ , None , lst ) -> List . iter lst ~ f : ( fun ( p , e ) -> add_labels_rec e ~ values ( : SMap . removes ( pattern_vars p ) values ) ) | Pexp_function ( _ , Some e , lst ) | Pexp_match ( e , lst ) | Pexp_try ( e , lst ) -> add_labels_rec e ; List . iter lst ~ f : ( fun ( p , e ) -> add_labels_rec e ~ values ( : SMap . removes ( pattern_vars p ) values ) ) | Pexp_apply ( e , args ) -> List . iter add_labels_rec ( e :: List . map snd args ) | Pexp_tuple l | Pexp_array l -> List . iter add_labels_rec l | Pexp_construct ( _ , Some e , _ ) | Pexp_variant ( _ , Some e ) | Pexp_field ( e , _ ) | Pexp_constraint ( e , _ , _ ) | Pexp_send ( e , _ ) | Pexp_setinstvar ( _ , e ) | Pexp_letmodule ( _ , _ , e ) | Pexp_assert e | Pexp_lazy e | Pexp_poly ( e , _ ) | Pexp_newtype ( _ , e ) | Pexp_open ( _ , e ) -> add_labels_rec e | Pexp_record ( lst , opt ) -> List . iter lst ~ f ( : fun ( _ , e ) -> add_labels_rec e ) ; begin match opt with Some e -> add_labels_rec e | None -> ( ) end | Pexp_setfield ( e1 , _ , e2 ) | Pexp_ifthenelse ( e1 , e2 , None ) | Pexp_sequence ( e1 , e2 ) | Pexp_while ( e1 , e2 ) | Pexp_when ( e1 , e2 ) -> add_labels_rec e1 ; add_labels_rec e2 | Pexp_ifthenelse ( e1 , e2 , Some e3 ) -> add_labels_rec e1 ; add_labels_rec e2 ; add_labels_rec e3 | Pexp_for ( s , e1 , e2 , _ , e3 ) -> add_labels_rec e1 ; add_labels_rec e2 ; add_labels_rec e3 ~ values ( : SMap . removes [ s ] values ) | Pexp_override lst -> List . iter lst ~ f ( : fun ( _ , e ) -> add_labels_rec e ) | Pexp_ident _ | Pexp_constant _ | Pexp_construct _ | Pexp_variant _ | Pexp_new _ | Pexp_assertfalse | Pexp_object _ | Pexp_pack _ -> ( )
let rec add_labels_class ~ text ~ classes ~ values ~ methods cl = match cl . pcl_desc with Pcl_constr _ -> ( ) | Pcl_structure ( p , l ) -> let values = SMap . removes ( pattern_vars p ) values in let values = match pattern_name p with None -> values | Some s -> List . fold_left methods ~ init ( : SMap . add s [ " < object " ] > values ) ~ f ( : fun m ( k , l ) -> SMap . add ( s " " ^#^ k ) l m ) in ignore ( List . fold_left l ~ init : values ~ f : begin fun values -> function | Pcf_val ( s , _ , _ , e , _ ) -> add_labels_expr ~ text ~ classes ~ values e ; SMap . removes [ s ] values | Pcf_meth ( s , _ , _ , e , _ ) -> begin try let labels = List . assoc s methods in insert_labels ~ labels ~ text e with Not_found -> ( ) end ; add_labels_expr ~ text ~ classes ~ values e ; values | Pcf_init e -> add_labels_expr ~ text ~ classes ~ values e ; values | Pcf_inher _ | Pcf_valvirt _ | Pcf_virt _ | Pcf_cstr _ -> values | Pcf_let _ -> values end ) | Pcl_fun ( _ , opt , pat , cl ) -> begin match opt with None -> ( ) | Some e -> add_labels_expr ~ text ~ classes ~ values e end ; let values = SMap . removes ( pattern_vars pat ) values in add_labels_class ~ text ~ classes ~ values ~ methods cl | Pcl_apply ( cl , args ) -> List . iter args ~ f ( : fun ( _ , e ) -> add_labels_expr ~ text ~ classes ~ values e ) ; add_labels_class ~ text ~ classes ~ values ~ methods cl | Pcl_let ( recp , lst , cl ) -> let vars = List . concat ( List . map lst ~ f ( : fun ( p , _ ) -> pattern_vars p ) ) in let vals = SMap . removes vars values in List . iter lst ~ f : begin fun ( _ , e ) -> add_labels_expr e ~ text ~ classes ~ values ( : if recp = Recursive then vals else values ) end ; add_labels_class cl ~ text ~ classes ~ values : vals ~ methods | Pcl_constraint ( cl , _ ) -> add_labels_class ~ text ~ classes ~ values ~ methods cl
let add_labels ~ intf ~ impl ~ file = insertions := [ ] ; let values , classes = List . fold_left intf ~ init ( : SMap . empty , SMap . empty ) ~ f : begin fun ( values , classes as acc ) item -> match item . psig_desc with Psig_value ( name , { pval_type = sty } ) -> ( SMap . add name ( labels_of_sty sty ) values , classes ) | Psig_class l -> ( values , List . fold_left l ~ init : classes ~ f : begin fun classes { pci_name = name ; pci_expr = cty } -> SMap . add name ( labels_of_cty cty ) classes end ) | _ -> acc end in let text = input_file file in ignore ( List . fold_right impl ~ init ( : values , classes ) ~ f : begin fun item ( values , classes as acc ) -> match item . pstr_desc with Pstr_value ( recp , l ) -> let names = List . concat ( List . map l ~ f ( : fun ( p , _ ) -> pattern_vars p ) ) in List . iter l ~ f : begin fun ( pat , expr ) -> begin match pattern_name pat with | Some s -> begin try let labels = SMap . find s values in insert_labels ~ labels ~ text expr ; if ! norec then ( ) else let values = SMap . fold ( fun s l m -> if List . mem s names then SMap . add s l m else m ) values SMap . empty in add_labels_expr expr ~ text ~ values ~ classes : SMap . empty with Not_found -> ( ) end | None -> ( ) end ; end ; ( SMap . removes names values , classes ) | Pstr_primitive ( s , { pval_type = sty } ) -> begin try let labels = SMap . find s values in insert_labels_type ~ labels ~ text sty ; ( SMap . removes [ s ] values , classes ) with Not_found -> acc end | Pstr_class l -> let names = List . map l ~ f ( : fun pci -> pci . pci_name ) in List . iter l ~ f : begin fun { pci_name = name ; pci_expr = expr } -> try let ( labels , methods ) = SMap . find name classes in insert_labels_class ~ labels ~ text expr ; if ! norec then ( ) else let classes = SMap . fold ( fun s ( l , _ ) m -> if List . mem s names then SMap . add s l m else m ) classes SMap . empty in add_labels_class expr ~ text ~ classes ~ methods ~ values : SMap . empty with Not_found -> ( ) end ; ( values , SMap . removes names classes ) | _ -> acc end ) ; if ! insertions <> [ ] then begin let backup = file ^ " . bak " in if Sys . file_exists backup then Sys . remove file else Sys . rename file backup ; let oc = open_out file in let last_pos = List . fold_left ( sort_insertions ( ) ) ~ init : 0 ~ f : begin fun pos ( pos ' , s ) -> output oc text pos ( pos ' - pos ) ; output_string oc s ; pos ' end in if last_pos < String . length text then output oc text last_pos ( String . length text - last_pos ) ; close_out oc end else prerr_endline ( " No labels to insert in " ^ file )
let process_file file = prerr_endline ( " Processing " ^ file ) ; if Filename . check_suffix file " . ml " then let intf = Filename . chop_suffix file " . ml " ^ " . mli " in let ic = open_in intf in let lexbuf = Lexing . from_channel ic in Location . init lexbuf intf ; let intf = Parse . interface lexbuf in close_in ic ; let ic = open_in file in let lexbuf = Lexing . from_channel ic in Location . init lexbuf file ; let impl = Parse . implementation lexbuf in close_in ic ; add_labels ~ intf ~ impl ~ file else prerr_endline ( file ^ " is not an implementation " )
let main ( ) = let files = ref [ ] in Arg . parse [ " - norec " , Arg . Set norec , " do not labelize recursive calls " ] ( fun f -> files := f :: ! files ) " addlabels [ - norec ] < files " ; > let files = List . rev ! files in List . iter files ~ f : process_file
let ( ) = main ( )
let size t = Dwarf_value . size ( Dwarf_value . uleb128 ( Uint64 . of_nonnegative_int64_exn t ) )
let emit ~ asm_directives ? comment t = Dwarf_value . emit ~ asm_directives ( Dwarf_value . uleb128 ? comment ( Uint64 . of_nonnegative_int64_exn t ) ) type nonrec t = t let compare = Stdlib . compare let hash = Hashtbl . hash let equal t1 t2 = compare t1 t2 = 0 let print ppf t = Format . fprintf ppf " % Ld " t let output _ _ = Misc . fatal_error " Not yet implemented " end )
module Pair = struct type nonrec t = t * t include Identifiable . Make ( struct type nonrec t = t let compare = Stdlib . compare let hash = Hashtbl . hash let equal t1 t2 = compare t1 t2 = 0 let print ppf ( x , y ) = Format . fprintf ppf " ( % Ld , % Ld ) " x y let output _ _ = Misc . fatal_error " Not yet implemented " end ) end
module Entry = struct type t = { addr : Asm_label . t ; adjustment : int } include Identifiable . Make ( struct type nonrec t = t let compare { addr = addr1 ; adjustment = adjustment1 } { addr = addr2 ; adjustment = adjustment2 } = let c = Asm_label . compare addr1 addr2 in if c <> 0 then c else Stdlib . compare adjustment1 adjustment2 let equal t1 t2 = compare t1 t2 = 0 let hash { addr ; adjustment } = Hashtbl . hash ( Asm_label . hash addr , adjustment ) let print _ _ = Misc . fatal_error " Not yet implemented " let output _ _ = Misc . fatal_error " Not yet implemented " end ) end
type entry_and_soc_symbol = { entry : Entry . t ; start_of_code_symbol : Asm_symbol . t }
type t = { base_addr : Asm_label . t ; mutable next_index : Address_index . t ; mutable table : entry_and_soc_symbol Address_index . Map . t ; mutable rev_table : Address_index . t Entry . Map . t }
let create ( ) = { base_addr = Asm_label . create ( DWARF Debug_addr ) ; next_index = Address_index . zero ; table = Address_index . Map . empty ; rev_table = Entry . Map . empty }
let add ( ? adjustment = 0 ) t ~ start_of_code_symbol addr = let entry : Entry . t = { addr ; adjustment } in match Entry . Map . find entry t . rev_table with | exception Not_found -> let index = t . next_index in t . next_index <- Address_index . succ index ; t . rev_table <- Entry . Map . add entry index t . rev_table ; let entry : entry_and_soc_symbol = { entry ; start_of_code_symbol } in t . table <- Address_index . Map . add index entry t . table ; index | index -> index
let base_addr t = t . base_addr
let initial_length t = let num_entries = Int64 . of_int ( Address_index . Map . cardinal t . table ) in let size_entries = Int64 . mul num_entries ( Int64 . of_int Dwarf_arch_sizes . size_addr ) in Initial_length . create ( Dwarf_int . of_int64_exn ( Int64 . add 4L size_entries ) )

No dataset card yet

New: Create and edit this dataset card directly on the website!

Contribute a Dataset Card
Downloads last month
0
Add dataset card