text
stringlengths
0
601k
let copy_object_file oc name = let file_name = try Load_path . find name with Not_found -> raise ( Error ( File_not_found name ) ) in let ic = open_in_bin file_name in try let buffer = really_input_string ic ( String . length cmo_magic_number ) in if buffer = cmo_magic_number then begin let compunit_pos = input_binary_...
let create_archive file_list lib_name = let outchan = open_out_bin lib_name in Misc . try_finally ~ always ( : fun ( ) -> close_out outchan ) ~ exceptionally ( : fun ( ) -> remove_file lib_name ) ( fun ( ) -> output_string outchan cma_magic_number ; let ofs_pos_toc = pos_out outchan in output_binary_int outchan 0 ; let...
let report_error ppf = function | File_not_found name -> fprintf ppf " Cannot find file % s " name | Not_an_object_file name -> fprintf ppf " The file % a is not a bytecode object file " Location . print_filename name
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
let reset ( ) = lib_ccobjs := [ ] ; lib_ccopts := [ ] ; lib_dllibs := [ ]
type error = | File_not_found of filepath | Not_an_object_file of filepath | Wrong_object_name of filepath | Symbol_error of filepath * Symtable . error | Inconsistent_import of modname * filepath * filepath | Custom_runtime | File_exists of filepath | Cannot_open_dll of filepath | Required_module_unavailable of modnam...
type link_action = Link_object of string * compilation_unit | Link_archive of string * compilation_unit list
let lib_ccobjs = ref [ ]
let lib_ccopts = ref [ ]
let lib_dllibs = ref [ ]
let add_ccobjs origin l = if not ! Clflags . no_auto_link then begin if String . length ! Clflags . use_runtime = 0 && String . length ! Clflags . use_prims = 0 then begin if l . lib_custom then Clflags . custom_runtime := true ; lib_ccobjs := l . lib_ccobjs @ ! lib_ccobjs ; let replace_origin = Misc . replace_substrin...
let missing_globals = ref Ident . Map . empty
let is_required ( rel , _pos ) = match rel with Reloc_setglobal id -> Ident . Map . mem id ! missing_globals | _ -> false
let add_required compunit = let add id = missing_globals := Ident . Map . add id compunit . cu_name ! missing_globals in List . iter add ( Symtable . required_globals compunit . cu_reloc ) ; List . iter add compunit . cu_required_globals
let remove_required ( rel , _pos ) = match rel with Reloc_setglobal id -> missing_globals := Ident . Map . remove id ! missing_globals | _ -> ( )
let scan_file obj_name tolink = let file_name = try Load_path . find obj_name with Not_found -> raise ( Error ( File_not_found obj_name ) ) in let ic = open_in_bin file_name in try let buffer = really_input_string ic ( String . length cmo_magic_number ) in if buffer = cmo_magic_number then begin let compunit_pos = inpu...
module Consistbl = Consistbl . Make ( Misc . Stdlib . String )
let crc_interfaces = Consistbl . create ( )
let interfaces = ref ( [ ] : string list )
let implementations_defined = ref ( [ ] : ( string * string ) list )
let check_consistency file_name cu = begin try List . iter ( fun ( name , crco ) -> interfaces := name :: ! interfaces ; match crco with None -> ( ) | Some crc -> if name = cu . cu_name then Consistbl . set crc_interfaces name crc file_name else Consistbl . check crc_interfaces name crc file_name ) cu . cu_imports with...
let extract_crc_interfaces ( ) = Consistbl . extract ! interfaces crc_interfaces
let clear_crc_interfaces ( ) = Consistbl . clear crc_interfaces ; interfaces := [ ]
let debug_info = ref ( [ ] : ( int * Instruct . debug_event list * string list ) list )
let link_compunit output_fun currpos_fun inchan file_name compunit = check_consistency file_name compunit ; seek_in inchan compunit . cu_pos ; let code_block = LongString . input_bytes inchan compunit . cu_codesize in Symtable . patch_object code_block compunit . cu_reloc ; if ! Clflags . debug && compunit . cu_debug >...
let link_object output_fun currpos_fun file_name compunit = let inchan = open_in_bin file_name in try link_compunit output_fun currpos_fun inchan file_name compunit ; close_in inchan with Symtable . Error msg -> close_in inchan ; raise ( Error ( Symbol_error ( file_name , msg ) ) ) | x -> close_in inchan ; raise x
let link_archive output_fun currpos_fun file_name units_required = let inchan = open_in_bin file_name in try List . iter ( fun cu -> let name = file_name ^ " ( " ^ cu . cu_name ^ " ) " in try link_compunit output_fun currpos_fun inchan name cu with Symtable . Error msg -> raise ( Error ( Symbol_error ( name , msg ) ) )...
let link_file output_fun currpos_fun = function Link_object ( file_name , unit ) -> link_object output_fun currpos_fun file_name unit | Link_archive ( file_name , units ) -> link_archive output_fun currpos_fun file_name units
let output_debug_info oc = output_binary_int oc ( List . length ! debug_info ) ; List . iter ( fun ( ofs , evl , debug_dirs ) -> output_binary_int oc ofs ; output_value oc evl ; output_value oc debug_dirs ) ! debug_info ; debug_info := [ ]
let output_stringlist oc l = List . iter ( fun s -> output_string oc s ; output_byte oc 0 ) l
let make_absolute file = if not ( Filename . is_relative file ) then file else Location . rewrite_absolute_path ( Filename . concat ( Sys . getcwd ( ) ) file )
let link_bytecode ? final_name tolink exec_name standalone = let final_name = Option . value final_name ~ default : exec_name in List . iter ( function | Link_object ( file_name , _ ) when file_name = exec_name -> raise ( Error ( Wrong_object_name exec_name ) ) ; | _ -> ( ) ) tolink ; Misc . remove_file exec_name ; let...
let output_code_string_counter = ref 0
let output_code_string outchan code = let pos = ref 0 in let len = Bytes . length code in while ! pos < len do let c1 = Char . code ( Bytes . get code ! pos ) in let c2 = Char . code ( Bytes . get code ( ! pos + 1 ) ) in let c3 = Char . code ( Bytes . get code ( ! pos + 2 ) ) in let c4 = Char . code ( Bytes . get code ...
let output_data_string outchan data = let counter = ref 0 in for i = 0 to String . length data - 1 do Printf . fprintf outchan " % d , " ( Char . code ( data . [ i ] ) ) ; incr counter ; if ! counter >= 12 then begin output_string outchan " \ n " ; counter := 0 end done
let output_cds_file outfile = Misc . remove_file outfile ; let outchan = open_out_gen [ Open_wronly ; Open_trunc ; Open_creat ; Open_binary ] 0o777 outfile in Misc . try_finally ~ always ( : fun ( ) -> close_out outchan ) ~ exceptionally ( : fun ( ) -> remove_file outfile ) ( fun ( ) -> Bytesections . init_record outch...
let link_bytecode_as_c tolink outfile with_main = let outchan = open_out outfile in Misc . try_finally ~ always ( : fun ( ) -> close_out outchan ) ~ exceptionally ( : fun ( ) -> remove_file outfile ) ( fun ( ) -> output_string outchan " \ \ n \ \ n # endif \ output_string outchan " static int caml_code [ ] = { \ n " ; ...
let build_custom_runtime prim_name exec_name = let runtime_lib = if not ! Clflags . with_runtime then " " else " - lcamlrun " ^ ! Clflags . runtime_variant in let debug_prefix_map = if Config . c_has_debug_prefix_map && not ! Clflags . keep_camlprimc_file then let flag = [ Printf . sprintf " - fdebug - prefix - map =% ...
let append_bytecode bytecode_name exec_name = let oc = open_out_gen [ Open_wronly ; Open_append ; Open_binary ] 0 exec_name in let ic = open_in_bin bytecode_name in copy_file ic oc ; close_in ic ; close_out oc
let fix_exec_name name = match Sys . os_type with " Win32 " | " Cygwin " -> if String . contains name ' . ' then name else name ^ " . exe " | _ -> name
let link objfiles output_name = let objfiles = match ! Clflags . nopervasives , ! Clflags . output_c_object , ! Clflags . output_complete_executable with | true , _ , _ -> objfiles | false , true , false -> " stdlib . cma " :: objfiles | _ -> " stdlib . cma " :: objfiles @ [ " std_exit . cmo " ] in let tolink = List . ...
let report_error ppf = function | File_not_found name -> fprintf ppf " Cannot find file % a " Location . print_filename name | Not_an_object_file name -> fprintf ppf " The file % a is not a bytecode object file " Location . print_filename name | Wrong_object_name name -> fprintf ppf " The output file % s has the wrong ...
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
let reset ( ) = lib_ccobjs := [ ] ; lib_ccopts := [ ] ; lib_dllibs := [ ] ; missing_globals := Ident . Map . empty ; Consistbl . clear crc_interfaces ; implementations_defined := [ ] ; debug_info := [ ] ; output_code_string_counter := 0
type error = Forward_reference of string * Ident . t | Multiple_definition of string * Ident . t | Not_an_object_file of string | Illegal_renaming of string * string * string | File_not_found of string
let relocs = ref ( [ ] : ( reloc_info * int ) list )
let events = ref ( [ ] : debug_event list )
let debug_dirs = ref String . Set . empty
let primitives = ref ( [ ] : string list )
let force_link = ref false
let rename_relocation packagename objfile mapping defined base ( rel , ofs ) = let rel ' = match rel with Reloc_getglobal id -> begin try let id ' = List . assoc id mapping in if List . mem id defined then Reloc_getglobal id ' else raise ( Error ( Forward_reference ( objfile , id ) ) ) with Not_found -> let name = Iden...
let relocate_debug base prefix subst ev = let ev ' = { ev with ev_pos = base + ev . ev_pos ; ev_module = prefix ^ " . " ^ ev . ev_module ; ev_typsubst = Subst . compose ev . ev_typsubst subst } in events := ev ' :: ! events
type pack_member_kind = PM_intf | PM_impl of compilation_unit
type pack_member = { pm_file : string ; pm_name : string ; pm_kind : pack_member_kind }
let read_member_info file = ( let name = String . capitalize_ascii ( Filename . basename ( chop_extensions file ) ) in let kind = if Filename . check_suffix file " . cmi " then PM_intf else begin let ic = open_in_bin file in try let buffer = really_input_string ic ( String . length Config . cmo_magic_number ) in if buf...
let rename_append_bytecode packagename oc mapping defined ofs prefix subst objfile compunit = let ic = open_in_bin objfile in try Bytelink . check_consistency objfile compunit ; List . iter ( rename_relocation packagename objfile mapping defined ofs ) compunit . cu_reloc ; primitives := compunit . cu_primitives @ ! pri...
let rec rename_append_bytecode_list packagename oc mapping defined ofs prefix subst = function [ ] -> ofs | m :: rem -> match m . pm_kind with | PM_intf -> rename_append_bytecode_list packagename oc mapping defined ofs prefix subst rem | PM_impl compunit -> let size = rename_append_bytecode packagename oc mapping defin...
let build_global_target ~ ppf_dump oc target_name members mapping pos coercion = let components = List . map2 ( fun m ( _id1 , id2 ) -> match m . pm_kind with | PM_intf -> None | PM_impl _ -> Some id2 ) members mapping in let lam = Translmod . transl_package components ( Ident . create_persistent target_name ) coercion...
let package_object_files ~ ppf_dump files targetfile targetname coercion = let members = map_left_right read_member_info files in let required_globals = List . fold_right ( fun compunit required_globals -> match compunit with | { pm_kind = PM_intf } -> required_globals | { pm_kind = PM_impl { cu_required_globals ; cu_r...
let package_files ~ ppf_dump initial_env files targetfile = let files = List . map ( fun f -> try Load_path . find f with Not_found -> raise ( Error ( File_not_found f ) ) ) files in let prefix = chop_extensions targetfile in let targetcmi = prefix ^ " . cmi " in let targetname = String . capitalize_ascii ( Filename . ...
let report_error ppf = function Forward_reference ( file , ident ) -> fprintf ppf " Forward reference to % s in file % a " ( Ident . name ident ) Location . print_filename file | Multiple_definition ( file , ident ) -> fprintf ppf " File % a redefines % s " Location . print_filename file ( Ident . name ident ) | Not_an...
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
let reset ( ) = relocs := [ ] ; events := [ ] ; primitives := [ ] ; force_link := false
[ @@@ ocaml . flambda_o3 ] = " caml_fill_bytes " [ @@ noalloc ] = " caml_blit_bytes " [ @@ noalloc ] = " caml_blit_string " [ @@ noalloc ]
let make n c = let s = create n in unsafe_fill s 0 n c ; s
let init n f = let s = create n in for i = 0 to n - 1 do unsafe_set s i ( f i ) done ; s
let empty = create 0
let copy s = let len = length s in let r = create len in unsafe_blit s 0 r 0 len ; r
let to_string b = unsafe_to_string ( copy b )
let of_string s = copy ( unsafe_of_string s )
let sub s ofs len = if ofs < 0 || len < 0 || ofs > length s - len then invalid_arg " String . sub / Bytes . sub " else begin let r = create len in unsafe_blit s ofs r 0 len ; r end
let sub_string b ofs len = unsafe_to_string ( sub b ofs len )
let ( ) ++ a b = let c = a + b in match a < 0 , b < 0 , c < 0 with | true , true , false | false , false , true -> invalid_arg " Bytes . extend " | _ -> c
let extend s left right = let len = length s ++ left ++ right in let r = create len in let ( srcoff , dstoff ) = if left < 0 then - left , 0 else 0 , left in let cpylen = min ( length s - srcoff ) ( len - dstoff ) in if cpylen > 0 then unsafe_blit s srcoff r dstoff cpylen ; r
let fill s ofs len c = if ofs < 0 || len < 0 || ofs > length s - len then invalid_arg " String . fill / Bytes . fill " else unsafe_fill s ofs len c
let blit s1 ofs1 s2 ofs2 len = if len < 0 || ofs1 < 0 || ofs1 > length s1 - len || ofs2 < 0 || ofs2 > length s2 - len then invalid_arg " Bytes . blit " else unsafe_blit s1 ofs1 s2 ofs2 len
let blit_string s1 ofs1 s2 ofs2 len = if len < 0 || ofs1 < 0 || ofs1 > string_length s1 - len || ofs2 < 0 || ofs2 > length s2 - len then invalid_arg " String . blit / Bytes . blit_string " else unsafe_blit_string s1 ofs1 s2 ofs2 len
let iter f a = for i = 0 to length a - 1 do f ( unsafe_get a i ) done
let iteri f a = for i = 0 to length a - 1 do f i ( unsafe_get a i ) done
let ensure_ge ( x : int ) y = if x >= y then x else invalid_arg " Bytes . concat "
let rec sum_lengths acc seplen = function | [ ] -> acc | hd :: [ ] -> length hd + acc | hd :: tl -> sum_lengths ( ensure_ge ( length hd + seplen + acc ) acc ) seplen tl
let rec unsafe_blits dst pos sep seplen = function [ ] -> dst | hd :: [ ] -> unsafe_blit hd 0 dst pos ( length hd ) ; dst | hd :: tl -> unsafe_blit hd 0 dst pos ( length hd ) ; unsafe_blit sep 0 dst ( pos + length hd ) seplen ; unsafe_blits dst ( pos + length hd + seplen ) sep seplen tl
let concat sep = function [ ] -> empty | l -> let seplen = length sep in unsafe_blits ( create ( sum_lengths 0 seplen l ) ) 0 sep seplen l
let cat s1 s2 = let l1 = length s1 in let l2 = length s2 in let r = create ( l1 + l2 ) in unsafe_blit s1 0 r 0 l1 ; unsafe_blit s2 0 r l1 l2 ; r
let is_space = function | ' ' | ' \ 012 ' | ' \ n ' | ' \ r ' | ' \ t ' -> true | _ -> false
let trim s = let len = length s in let i = ref 0 in while ! i < len && is_space ( unsafe_get s ! i ) do incr i done ; let j = ref ( len - 1 ) in while ! j >= ! i && is_space ( unsafe_get s ! j ) do decr j done ; if ! j >= ! i then sub s ! i ( ! j - ! i + 1 ) else empty
let escaped s = let n = ref 0 in for i = 0 to length s - 1 do n := ! n + ( match unsafe_get s i with | ' " ' \ | ' ' \\ | ' \ n ' | ' \ t ' | ' \ r ' | ' \ b ' -> 2 | ' ' . . ' ' ~ -> 1 | _ -> 4 ) done ; if ! n = length s then copy s else begin let s ' = create ! n in n := 0 ; for i = 0 to length s - 1 do begin match u...
let map f s = let l = length s in if l = 0 then s else begin let r = create l in for i = 0 to l - 1 do unsafe_set r i ( f ( unsafe_get s i ) ) done ; r end
let mapi f s = let l = length s in if l = 0 then s else begin let r = create l in for i = 0 to l - 1 do unsafe_set r i ( f i ( unsafe_get s i ) ) done ; r end
let uppercase_ascii s = map Char . uppercase_ascii s
let lowercase_ascii s = map Char . lowercase_ascii s
let apply1 f s = if length s = 0 then s else begin let r = copy s in unsafe_set r 0 ( f ( unsafe_get s 0 ) ) ; r end
let capitalize_ascii s = apply1 Char . uppercase_ascii s
let uncapitalize_ascii s = apply1 Char . lowercase_ascii s
let rec index_rec s lim i c = if i >= lim then raise Not_found else if unsafe_get s i = c then i else index_rec s lim ( i + 1 ) c
let index s c = index_rec s ( length s ) 0 c
let rec index_rec_opt s lim i c = if i >= lim then None else if unsafe_get s i = c then Some i else index_rec_opt s lim ( i + 1 ) c
let index_opt s c = index_rec_opt s ( length s ) 0 c
let index_from s i c = let l = length s in if i < 0 || i > l then invalid_arg " String . index_from / Bytes . index_from " else index_rec s l i c
let index_from_opt s i c = let l = length s in if i < 0 || i > l then invalid_arg " String . index_from_opt / Bytes . index_from_opt " else index_rec_opt s l i c