text
stringlengths
12
786k
let ( ) ++ x f = f x
let compile_fundecl ~ ppf_dump fd_cmm = Proc . init ( ) ; Reg . reset ( ) ; fd_cmm ++ Profile . record ~ accumulate : true " cmm_invariants " ( cmm_invariants ppf_dump ) ++ Profile . record ~ accumulate : true " selection " Selection . fundecl ++ pass_dump_if ppf_dump dump_selection " After instruction selection " ++ Profile . record ~ accumulate : true " comballoc " Comballoc . fundecl ++ pass_dump_if ppf_dump dump_combine " After allocation combining " ++ Profile . record ~ accumulate : true " cse " CSE . fundecl ++ pass_dump_if ppf_dump dump_cse " After CSE " ++ Profile . record ~ accumulate : true " liveness " liveness ++ Profile . record ~ accumulate : true " deadcode " Deadcode . fundecl ++ pass_dump_if ppf_dump dump_live " Liveness analysis " ++ Profile . record ~ accumulate : true " spill " Spill . fundecl ++ Profile . record ~ accumulate : true " liveness " liveness ++ pass_dump_if ppf_dump dump_spill " After spilling " ++ Profile . record ~ accumulate : true " split " Split . fundecl ++ pass_dump_if ppf_dump dump_split " After live range splitting " ++ Profile . record ~ accumulate : true " liveness " liveness ++ Profile . record ~ accumulate : true " regalloc " ( regalloc ~ ppf_dump 1 ) ++ Profile . record ~ accumulate : true " available_regs " Available_regs . fundecl ++ Profile . record ~ accumulate : true " linearize " Linearize . fundecl ++ pass_dump_linear_if ppf_dump dump_linear " Linearized code " ++ Profile . record ~ accumulate : true " scheduling " Scheduling . fundecl ++ pass_dump_linear_if ppf_dump dump_scheduling " After instruction scheduling " ++ save_linear ++ emit_fundecl
let compile_data dl = dl ++ save_data ++ emit_data
let compile_phrase ~ ppf_dump p = if ! dump_cmm then fprintf ppf_dump " % a . " @ Printcmm . phrase p ; match p with | Cfunction fd -> compile_fundecl ~ ppf_dump fd | Cdata dl -> compile_data dl
let compile_genfuns ~ ppf_dump f = List . iter ( function | ( Cfunction { fun_name = name } ) as ph when f name -> compile_phrase ~ ppf_dump ph | _ -> ( ) ) ( Cmm_helpers . generic_functions true [ Compilenv . current_unit_infos ( ) ] )
let compile_unit ~ output_prefix ~ asm_filename ~ keep_asm ~ obj_filename gen = reset ( ) ; let create_asm = should_emit ( ) && ( keep_asm || not ! Emitaux . binary_backend_available ) in Emitaux . create_asm_file := create_asm ; Misc . try_finally ~ exceptionally ( : fun ( ) -> remove_file obj_filename ) ( fun ( ) -> if create_asm then Emitaux . output_channel := open_out asm_filename ; Misc . try_finally ( fun ( ) -> gen ( ) ; write_linear output_prefix ) ~ always ( : fun ( ) -> if create_asm then close_out ! Emitaux . output_channel ) ~ exceptionally ( : fun ( ) -> if create_asm && not keep_asm then remove_file asm_filename ) ; if should_emit ( ) then begin let assemble_result = Profile . record " assemble " ( Proc . assemble_file asm_filename ) obj_filename in if assemble_result <> 0 then raise ( Error ( Assembler_error asm_filename ) ) ; end ; if create_asm && not keep_asm then remove_file asm_filename )
let end_gen_implementation ? toplevel ~ ppf_dump ( clambda : Clambda . with_constants ) = emit_begin_assembly ( ) ; clambda ++ Profile . record " cmm " Cmmgen . compunit ++ Profile . record " compile_phrases " ( List . iter ( compile_phrase ~ ppf_dump ) ) ++ ( fun ( ) -> ( ) ) ; ( match toplevel with None -> ( ) | Some f -> compile_genfuns ~ ppf_dump f ) ; compile_phrase ~ ppf_dump ( Cmm_helpers . reference_symbols ( List . filter_map ( fun prim -> if not ( Primitive . native_name_is_external prim ) then None else Some ( Primitive . native_name prim ) ) ! Translmod . primitive_declarations ) ) ; emit_end_assembly ( )
type middle_end = backend ( : module Backend_intf . S ) -> filename : string -> prefixname : string -> ppf_dump : Format . formatter -> Lambda . program -> Clambda . with_constants
let asm_filename output_prefix = if ! keep_asm_file || ! Emitaux . binary_backend_available then output_prefix ^ ext_asm else Filename . temp_file " camlasm " ext_asm
let compile_implementation ? toplevel ~ backend ~ filename ~ prefixname ~ middle_end ~ ppf_dump ( program : Lambda . program ) = compile_unit ~ output_prefix : prefixname ~ asm_filename ( : asm_filename prefixname ) ~ keep_asm :! keep_asm_file ~ obj_filename ( : prefixname ^ ext_obj ) ( fun ( ) -> Ident . Set . iter Compilenv . require_global program . required_globals ; let clambda_with_constants = middle_end ~ backend ~ filename ~ prefixname ~ ppf_dump program in end_gen_implementation ? toplevel ~ ppf_dump clambda_with_constants )
let linear_gen_implementation filename = let open Linear_format in let linear_unit_info , _ = restore filename in ( match ! Clflags . for_package , linear_unit_info . for_pack with | None , None -> ( ) | Some expected , Some saved when String . equal expected saved -> ( ) | _ , saved -> raise ( Error ( Mismatched_for_pack saved ) ) ) ; let emit_item = function | Data dl -> emit_data dl | Func f -> emit_fundecl f in start_from_emit := true ; emit_begin_assembly ( ) ; Profile . record " Emit " ( List . iter emit_item ) linear_unit_info . items ; emit_end_assembly ( )
let compile_implementation_linear output_prefix ~ progname = compile_unit ~ output_prefix ~ asm_filename ( : asm_filename output_prefix ) ~ keep_asm :! keep_asm_file ~ obj_filename ( : output_prefix ^ ext_obj ) ( fun ( ) -> linear_gen_implementation progname )
let report_error ppf = function | Assembler_error file -> fprintf ppf " Assembler error , input left in file % a " Location . print_filename file | Mismatched_for_pack saved -> let msg = function | None -> " without - for - pack " | Some s -> " with - for - pack " ^ s in fprintf ppf " This input file cannot be compiled % s : it was generated % s . " ( msg ! Clflags . for_package ) ( msg saved ) | Asm_generation ( fn , err ) -> fprintf ppf " Error producing assembly code for function % s : % a " fn Emitaux . report_error err
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
type error = File_not_found of string | Archiver_error of string
let default_ui_export_info = if Config . flambda then Cmx_format . Flambda Export_info . empty else Cmx_format . Clambda Clambda . Value_unknown
let read_info name = let filename = try Load_path . find name with Not_found -> raise ( Error ( File_not_found name ) ) in let ( info , crc ) = Compilenv . read_unit_info filename in info . ui_force_link <- info . ui_force_link || ! Clflags . link_everything ; info . ui_export_info <- default_ui_export_info ; ( Filename . chop_suffix filename " . cmx " ^ ext_obj , ( info , crc ) )
let create_archive file_list lib_name = let archive_name = Filename . remove_extension lib_name ^ ext_lib in let outchan = open_out_bin lib_name in Misc . try_finally ~ always ( : fun ( ) -> close_out outchan ) ~ exceptionally ( : fun ( ) -> remove_file lib_name ; remove_file archive_name ) ( fun ( ) -> output_string outchan cmxa_magic_number ; let ( objfile_list , descr_list ) = List . split ( List . map read_info file_list ) in List . iter2 ( fun file_name ( unit , crc ) -> Asmlink . check_consistency file_name unit crc ) file_list descr_list ; let infos = { lib_units = descr_list ; lib_ccobjs = ! Clflags . ccobjs ; lib_ccopts = ! Clflags . all_ccopts } in output_value outchan infos ; if Ccomp . create_archive archive_name objfile_list <> 0 then raise ( Error ( Archiver_error archive_name ) ) ; )
let report_error ppf = function | File_not_found name -> fprintf ppf " Cannot find file % s " name | Archiver_error name -> fprintf ppf " Error while creating the library % s " name
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
type error = | File_not_found of filepath | Not_an_object_file of filepath | Missing_implementations of ( modname * string list ) list | Inconsistent_interface of modname * filepath * filepath | Inconsistent_implementation of modname * filepath * filepath | Assembler_error of filepath | Linking_error of int | Multiple_definition of modname * filepath * filepath | Missing_cmx of filepath * modname
module Cmi_consistbl = Consistbl . Make ( Misc . Stdlib . String )
let crc_interfaces = Cmi_consistbl . create ( )
let interfaces = ref ( [ ] : string list )
module Cmx_consistbl = Consistbl . Make ( Misc . Stdlib . String )
let crc_implementations = Cmx_consistbl . create ( )
let implementations = ref ( [ ] : string list )
let implementations_defined = ref ( [ ] : ( string * string ) list )
let cmx_required = ref ( [ ] : string list )
let check_consistency file_name unit crc = begin try List . iter ( fun ( name , crco ) -> interfaces := name :: ! interfaces ; match crco with None -> ( ) | Some crc -> if name = unit . ui_name then Cmi_consistbl . set crc_interfaces name crc file_name else Cmi_consistbl . check crc_interfaces name crc file_name ) unit . ui_imports_cmi with Cmi_consistbl . Inconsistency { unit_name = name ; inconsistent_source = user ; original_source = auth ; } -> raise ( Error ( Inconsistent_interface ( name , user , auth ) ) ) end ; begin try List . iter ( fun ( name , crco ) -> implementations := name :: ! implementations ; match crco with None -> if List . mem name ! cmx_required then raise ( Error ( Missing_cmx ( file_name , name ) ) ) | Some crc -> Cmx_consistbl . check crc_implementations name crc file_name ) unit . ui_imports_cmx with Cmx_consistbl . Inconsistency { unit_name = name ; inconsistent_source = user ; original_source = auth ; } -> raise ( Error ( Inconsistent_implementation ( name , user , auth ) ) ) end ; begin try let source = List . assoc unit . ui_name ! implementations_defined in raise ( Error ( Multiple_definition ( unit . ui_name , file_name , source ) ) ) with Not_found -> ( ) end ; implementations := unit . ui_name :: ! implementations ; Cmx_consistbl . set crc_implementations unit . ui_name crc file_name ; implementations_defined := ( unit . ui_name , file_name ) :: ! implementations_defined ; if unit . ui_symbol <> unit . ui_name then cmx_required := unit . ui_name :: ! cmx_required
let extract_crc_interfaces ( ) = Cmi_consistbl . extract ! interfaces crc_interfaces
let extract_crc_implementations ( ) = Cmx_consistbl . extract ! implementations crc_implementations
let lib_ccobjs = ref [ ]
let lib_ccopts = ref [ ]
let add_ccobjs origin l = if not ! Clflags . no_auto_link then begin lib_ccobjs := l . lib_ccobjs @ ! lib_ccobjs ; let replace_origin = Misc . replace_substring ~ before " :$ CAMLORIGIN " ~ after : origin in lib_ccopts := List . map replace_origin l . lib_ccopts @ ! lib_ccopts end
let runtime_lib ( ) = let libname = " libasmrun " ^ ! Clflags . runtime_variant ^ ext_lib in try if ! Clflags . nopervasives || not ! Clflags . with_runtime then [ ] else [ Load_path . find libname ] with Not_found -> raise ( Error ( File_not_found libname ) )
let missing_globals = ( Hashtbl . create 17 : ( string , string list ref ) Hashtbl . t )
let is_required name = try ignore ( Hashtbl . find missing_globals name ) ; true with Not_found -> false
let add_required by ( name , _crc ) = try let rq = Hashtbl . find missing_globals name in rq := by :: ! rq with Not_found -> Hashtbl . add missing_globals name ( ref [ by ] )
let remove_required name = Hashtbl . remove missing_globals name
let extract_missing_globals ( ) = let mg = ref [ ] in Hashtbl . iter ( fun md rq -> mg := ( md , ! rq ) :: ! mg ) missing_globals ; ! mg
type file = | Unit of string * unit_infos * Digest . t | Library of string * library_infos
let object_file_name_of_file = function | Unit ( fname , _ , _ ) -> Some ( Filename . chop_suffix fname " . cmx " ^ ext_obj ) | Library ( fname , infos ) -> let obj_file = Filename . chop_suffix fname " . cmxa " ^ ext_lib in if infos . lib_units = [ ] && not ( Sys . file_exists obj_file ) then None else Some obj_file
let read_file obj_name = let file_name = try Load_path . find obj_name with Not_found -> raise ( Error ( File_not_found obj_name ) ) in if Filename . check_suffix file_name " . cmx " then begin let ( info , crc ) = read_unit_info file_name in Unit ( file_name , info , crc ) end else if Filename . check_suffix file_name " . cmxa " then begin let infos = try read_library_info file_name with Compilenv . Error ( Not_a_unit_info _ ) -> raise ( Error ( Not_an_object_file file_name ) ) in Library ( file_name , infos ) end else raise ( Error ( Not_an_object_file file_name ) )
let scan_file file tolink = match file with | Unit ( file_name , info , crc ) -> remove_required info . ui_name ; List . iter ( add_required file_name ) info . ui_imports_cmx ; ( info , file_name , crc ) :: tolink | Library ( file_name , infos ) -> add_ccobjs ( Filename . dirname file_name ) infos ; List . fold_right ( fun ( info , crc ) reqd -> if info . ui_force_link || ! Clflags . link_everything || is_required info . ui_name then begin remove_required info . ui_name ; List . iter ( add_required ( Printf . sprintf " % s ( % s ) " file_name info . ui_name ) ) info . ui_imports_cmx ; ( info , file_name , crc ) :: reqd end else reqd ) infos . lib_units tolink
let force_linking_of_startup ~ ppf_dump = Asmgen . compile_phrase ~ ppf_dump ( Cmm . Cdata ( [ Cmm . Csymbol_address " caml_startup " ] ) )
let make_globals_map units_list ~ crc_interfaces = let crc_interfaces = String . Tbl . of_seq ( List . to_seq crc_interfaces ) in let defined = List . map ( fun ( unit , _ , impl_crc ) -> let intf_crc = String . Tbl . find crc_interfaces unit . ui_name in String . Tbl . remove crc_interfaces unit . ui_name ; ( unit . ui_name , intf_crc , Some impl_crc , unit . ui_defines ) ) units_list in String . Tbl . fold ( fun name intf acc -> ( name , intf , None , [ ] ) :: acc ) crc_interfaces defined
let make_startup_file ~ ppf_dump units_list ~ crc_interfaces = let compile_phrase p = Asmgen . compile_phrase ~ ppf_dump p in Location . input_name := " caml_startup " ; Compilenv . reset " _startup " ; Emit . begin_assembly ( ) ; let name_list = List . flatten ( List . map ( fun ( info , _ , _ ) -> info . ui_defines ) units_list ) in compile_phrase ( Cmm_helpers . entry_point name_list ) ; let units = List . map ( fun ( info , _ , _ ) -> info ) units_list in List . iter compile_phrase ( Cmm_helpers . generic_functions false units ) ; Array . iteri ( fun i name -> compile_phrase ( Cmm_helpers . predef_exception i name ) ) Runtimedef . builtin_exceptions ; compile_phrase ( Cmm_helpers . global_table name_list ) ; let globals_map = make_globals_map units_list ~ crc_interfaces in compile_phrase ( Cmm_helpers . globals_map globals_map ) ; compile_phrase ( Cmm_helpers . data_segment_table ( " _startup " :: name_list ) ) ; if ! Clflags . function_sections then compile_phrase ( Cmm_helpers . code_segment_table ( " _hot " :: " _startup " :: name_list ) ) else compile_phrase ( Cmm_helpers . code_segment_table ( " _startup " :: name_list ) ) ; let all_names = " _startup " :: " _system " :: name_list in compile_phrase ( Cmm_helpers . frame_table all_names ) ; if ! Clflags . output_complete_object then force_linking_of_startup ~ ppf_dump ; Emit . end_assembly ( )
let make_shared_startup_file ~ ppf_dump units = let compile_phrase p = Asmgen . compile_phrase ~ ppf_dump p in Location . input_name := " caml_startup " ; Compilenv . reset " _shared_startup " ; Emit . begin_assembly ( ) ; List . iter compile_phrase ( Cmm_helpers . generic_functions true ( List . map fst units ) ) ; compile_phrase ( Cmm_helpers . plugin_header units ) ; compile_phrase ( Cmm_helpers . global_table ( List . map ( fun ( ui , _ ) -> ui . ui_symbol ) units ) ) ; if ! Clflags . output_complete_object then force_linking_of_startup ~ ppf_dump ; Emit . end_assembly ( )
let call_linker_shared file_list output_name = let exitcode = Ccomp . call_linker Ccomp . Dll output_name file_list " " in if not ( exitcode = 0 ) then raise ( Error ( Linking_error exitcode ) )
let link_shared ~ ppf_dump objfiles output_name = Profile . record_call output_name ( fun ( ) -> let obj_infos = List . map read_file objfiles in let units_tolink = List . fold_right scan_file obj_infos [ ] in List . iter ( fun ( info , file_name , crc ) -> check_consistency file_name info crc ) units_tolink ; Clflags . ccobjs := ! Clflags . ccobjs @ ! lib_ccobjs ; Clflags . all_ccopts := ! lib_ccopts @ ! Clflags . all_ccopts ; let objfiles = List . rev ( List . filter_map object_file_name_of_file obj_infos ) @ ( List . rev ! Clflags . ccobjs ) in let startup = if ! Clflags . keep_startup_file || ! Emitaux . binary_backend_available then output_name ^ " . startup " ^ ext_asm else Filename . temp_file " camlstartup " ext_asm in let startup_obj = output_name ^ " . startup " ^ ext_obj in Asmgen . compile_unit ~ output_prefix : output_name ~ asm_filename : startup ~ keep_asm :! Clflags . keep_startup_file ~ obj_filename : startup_obj ( fun ( ) -> make_shared_startup_file ~ ppf_dump ( List . map ( fun ( ui , _ , crc ) -> ( ui , crc ) ) units_tolink ) ) ; call_linker_shared ( startup_obj :: objfiles ) output_name ; remove_file startup_obj )
let call_linker file_list startup_file output_name = let main_dll = ! Clflags . output_c_object && Filename . check_suffix output_name Config . ext_dll and main_obj_runtime = ! Clflags . output_complete_object in let files = startup_file :: ( List . rev file_list ) in let files , c_lib = if ( not ! Clflags . output_c_object ) || main_dll || main_obj_runtime then files @ ( List . rev ! Clflags . ccobjs ) @ runtime_lib ( ) , ( if ! Clflags . nopervasives || ( main_obj_runtime && not main_dll ) then " " else Config . native_c_libraries ) else files , " " in let mode = if main_dll then Ccomp . MainDll else if ! Clflags . output_c_object then Ccomp . Partial else Ccomp . Exe in let exitcode = Ccomp . call_linker mode output_name files c_lib in if not ( exitcode = 0 ) then raise ( Error ( Linking_error exitcode ) )
let link ~ ppf_dump objfiles output_name = Profile . record_call output_name ( fun ( ) -> let stdlib = " stdlib . cmxa " in let stdexit = " std_exit . cmx " in let objfiles = if ! Clflags . nopervasives then objfiles else if ! Clflags . output_c_object then stdlib :: objfiles else stdlib :: ( objfiles @ [ stdexit ] ) in let obj_infos = List . map read_file objfiles in let units_tolink = List . fold_right scan_file obj_infos [ ] in Array . iter remove_required Runtimedef . builtin_exceptions ; begin match extract_missing_globals ( ) with [ ] -> ( ) | mg -> raise ( Error ( Missing_implementations mg ) ) end ; List . iter ( fun ( info , file_name , crc ) -> check_consistency file_name info crc ) units_tolink ; let crc_interfaces = extract_crc_interfaces ( ) in Clflags . ccobjs := ! Clflags . ccobjs @ ! lib_ccobjs ; Clflags . all_ccopts := ! lib_ccopts @ ! Clflags . all_ccopts ; let startup = if ! Clflags . keep_startup_file || ! Emitaux . binary_backend_available then output_name ^ " . startup " ^ ext_asm else Filename . temp_file " camlstartup " ext_asm in let startup_obj = Filename . temp_file " camlstartup " ext_obj in Asmgen . compile_unit ~ output_prefix : output_name ~ asm_filename : startup ~ keep_asm :! Clflags . keep_startup_file ~ obj_filename : startup_obj ( fun ( ) -> make_startup_file ~ ppf_dump units_tolink ~ crc_interfaces ) ; Misc . try_finally ( fun ( ) -> call_linker ( List . filter_map object_file_name_of_file obj_infos ) startup_obj output_name ) ~ always ( : fun ( ) -> remove_file startup_obj ) )
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 compilation unit description " Location . print_filename name | Missing_implementations l -> let print_references ppf = function | [ ] -> ( ) | r1 :: rl -> fprintf ppf " % s " r1 ; List . iter ( fun r -> fprintf ppf " , @ % s " r ) rl in let print_modules ppf = List . iter ( fun ( md , rq ) -> fprintf ppf " @ [ @< hov 2 >% s referenced from % a ] " @ md print_references rq ) in fprintf ppf " [ @< v 2 > No implementations provided for the following modules :% a ] " @ print_modules l | Inconsistent_interface ( intf , file1 , file2 ) -> fprintf ppf " [ @< hov > Files % a @ and % a @ make inconsistent assumptions \ over interface % s ] " @ Location . print_filename file1 Location . print_filename file2 intf | Inconsistent_implementation ( intf , file1 , file2 ) -> fprintf ppf " [ @< hov > Files % a @ and % a @ make inconsistent assumptions \ over implementation % s ] " @ Location . print_filename file1 Location . print_filename file2 intf | Assembler_error file -> fprintf ppf " Error while assembling % a " Location . print_filename file | Linking_error exitcode -> fprintf ppf " Error during linking ( exit code % d ) " exitcode | Multiple_definition ( modname , file1 , file2 ) -> fprintf ppf " [ @< hov > Files % a @ and % a @ both define a module named % s ] " @ Location . print_filename file1 Location . print_filename file2 modname | Missing_cmx ( filename , name ) -> fprintf ppf " [ @< hov > File % a @ was compiled without access @ \ to the . cmx file @ for module % s , @ \ which was produced by ` ocamlopt - for - pack ' . @ \ Please recompile % a @ with the correct ` - I ' option @ \ so that % s . cmx @ is found . ] " @ Location . print_filename filename name Location . print_filename filename name
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
let reset ( ) = Cmi_consistbl . clear crc_interfaces ; Cmx_consistbl . clear crc_implementations ; implementations_defined := [ ] ; cmx_required := [ ] ; interfaces := [ ] ; implementations := [ ] ; lib_ccobjs := [ ] ; lib_ccopts := [ ]
type error = Illegal_renaming of string * string * string | Forward_reference of string * string | Wrong_for_pack of string * string | Linking_error | Assembler_error of string | File_not_found of string
type pack_member_kind = PM_intf | PM_impl of unit_infos
type pack_member = { pm_file : string ; pm_name : string ; pm_kind : pack_member_kind }
let read_member_info pack_path 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 ( info , crc ) = Compilenv . read_unit_info file in if info . ui_name <> name then raise ( Error ( Illegal_renaming ( name , file , info . ui_name ) ) ) ; if info . ui_symbol <> ( Compilenv . current_unit_infos ( ) ) . ui_symbol ^ " __ " ^ info . ui_name then raise ( Error ( Wrong_for_pack ( file , pack_path ) ) ) ; Asmlink . check_consistency file info crc ; Compilenv . cache_unit_info info ; PM_impl info end in { pm_file = file ; pm_name = name ; pm_kind = kind } )
let check_units members = let rec check forbidden = function [ ] -> ( ) | mb :: tl -> begin match mb . pm_kind with | PM_intf -> ( ) | PM_impl infos -> List . iter ( fun ( unit , _ ) -> if List . mem unit forbidden then raise ( Error ( Forward_reference ( mb . pm_file , unit ) ) ) ) infos . ui_imports_cmx end ; check ( list_remove mb . pm_name forbidden ) tl in check ( List . map ( fun mb -> mb . pm_name ) members ) members
let make_package_object ~ ppf_dump members targetobj targetname coercion ~ backend = Profile . record_call ( Printf . sprintf " pack ( % s ) " targetname ) ( fun ( ) -> let objtemp = if ! Clflags . keep_asm_file then Filename . remove_extension targetobj ^ " . pack " ^ Config . ext_obj else Filename . temp_file ( Compilenv . make_symbol ( Some " " ) ) Config . ext_obj in let components = List . map ( fun m -> match m . pm_kind with | PM_intf -> None | PM_impl _ -> Some ( Ident . create_persistent m . pm_name ) ) members in let module_ident = Ident . create_persistent targetname in let prefixname = Filename . remove_extension objtemp in let required_globals = Ident . Set . empty in let program , middle_end = if Config . flambda then let main_module_block_size , code = Translmod . transl_package_flambda components coercion in let code = Simplif . simplify_lambda code in let program = { Lambda . code ; main_module_block_size ; module_ident ; required_globals ; } in program , Flambda_middle_end . lambda_to_clambda else let main_module_block_size , code = Translmod . transl_store_package components ( Ident . create_persistent targetname ) coercion in let code = Simplif . simplify_lambda code in let program = { Lambda . code ; main_module_block_size ; module_ident ; required_globals ; } in program , Closure_middle_end . lambda_to_clambda in Asmgen . compile_implementation ~ backend ~ filename : targetname ~ prefixname ~ middle_end ~ ppf_dump program ; let objfiles = List . map ( fun m -> Filename . remove_extension m . pm_file ^ Config . ext_obj ) ( List . filter ( fun m -> m . pm_kind <> PM_intf ) members ) in let exitcode = Ccomp . call_linker Ccomp . Partial targetobj ( objtemp :: objfiles ) " " in remove_file objtemp ; if not ( exitcode = 0 ) then raise ( Error Linking_error ) )
let get_export_info ui = assert ( Config . flambda ) ; match ui . ui_export_info with | Clambda _ -> assert false | Flambda info -> info
let get_approx ui = assert ( not Config . flambda ) ; match ui . ui_export_info with | Flambda _ -> assert false | Clambda info -> info
let build_package_cmx members cmxfile = let unit_names = List . map ( fun m -> m . pm_name ) members in let filter lst = List . filter ( fun ( name , _crc ) -> not ( List . mem name unit_names ) ) lst in let union lst = List . fold_left ( List . fold_left ( fun accu n -> if List . mem n accu then accu else n :: accu ) ) [ ] lst in let units = List . fold_right ( fun m accu -> match m . pm_kind with PM_intf -> accu | PM_impl info -> info :: accu ) members [ ] in let pack_units = List . fold_left ( fun set info -> let unit_id = Compilenv . unit_id_from_name info . ui_name in Compilation_unit . Set . add ( Compilenv . unit_for_global unit_id ) set ) Compilation_unit . Set . empty units in let units = if Config . flambda then List . map ( fun info -> { info with ui_export_info = Flambda ( Export_info_for_pack . import_for_pack ~ pack_units ~ pack ( : Compilenv . current_unit ( ) ) ( get_export_info info ) ) } ) units else units in let ui = Compilenv . current_unit_infos ( ) in let ui_export_info = if Config . flambda then let ui_export_info = List . fold_left ( fun acc info -> Export_info . merge acc ( get_export_info info ) ) ( Export_info_for_pack . import_for_pack ~ pack_units ~ pack ( : Compilenv . current_unit ( ) ) ( get_export_info ui ) ) units in Flambda ui_export_info else Clambda ( get_approx ui ) in Export_info_for_pack . clear_import_state ( ) ; let pkg_infos = { ui_name = ui . ui_name ; ui_symbol = ui . ui_symbol ; ui_defines = List . flatten ( List . map ( fun info -> info . ui_defines ) units ) @ [ ui . ui_symbol ] ; ui_imports_cmi = ( ui . ui_name , Some ( Env . crc_of_unit ui . ui_name ) ) :: filter ( Asmlink . extract_crc_interfaces ( ) ) ; ui_imports_cmx = filter ( Asmlink . extract_crc_implementations ( ) ) ; ui_curry_fun = union ( List . map ( fun info -> info . ui_curry_fun ) units ) ; ui_apply_fun = union ( List . map ( fun info -> info . ui_apply_fun ) units ) ; ui_send_fun = union ( List . map ( fun info -> info . ui_send_fun ) units ) ; ui_force_link = List . exists ( fun info -> info . ui_force_link ) units ; ui_export_info ; } in Compilenv . write_unit_info pkg_infos cmxfile
let package_object_files ~ ppf_dump files targetcmx targetobj targetname coercion ~ backend = let pack_path = match ! Clflags . for_package with | None -> targetname | Some p -> p ^ " . " ^ targetname in let members = map_left_right ( read_member_info pack_path ) files in check_units members ; make_package_object ~ ppf_dump members targetobj targetname coercion ~ backend ; build_package_cmx members targetcmx
let package_files ~ ppf_dump initial_env files targetcmx ~ backend = 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 targetcmx in let targetcmi = prefix ^ " . cmi " in let targetobj = Filename . remove_extension targetcmx ^ Config . ext_obj in let targetname = String . capitalize_ascii ( Filename . basename prefix ) in Location . input_name := targetcmx ; Compilenv . reset ? packname :! Clflags . for_package targetname ; Misc . try_finally ( fun ( ) -> let coercion = Typemod . package_units initial_env files targetcmi targetname in package_object_files ~ ppf_dump files targetcmx targetobj targetname coercion ~ backend ) ~ exceptionally ( : fun ( ) -> remove_file targetcmx ; remove_file targetobj )
let report_error ppf = function Illegal_renaming ( name , file , id ) -> fprintf ppf " Wrong file naming : % a @ contains the code for \ @ % s when % s was expected " Location . print_filename file name id | Forward_reference ( file , ident ) -> fprintf ppf " Forward reference to % s in file % a " ident Location . print_filename file | Wrong_for_pack ( file , path ) -> fprintf ppf " File % a @ was not compiled with the ` - for - pack % s ' option " Location . print_filename file path | File_not_found file -> fprintf ppf " File % s not found " file | Assembler_error file -> fprintf ppf " Error while assembling % s " file | Linking_error -> fprintf ppf " Error during partial linking "
let ( ) = Location . register_error_of_exn ( function | Error err -> Some ( Location . error_of_printer_file report_error err ) | _ -> None )
module type S = Asm_directives_intf . S
module type Arg = Asm_directives_intf . Arg
module Cached_string = struct type t = { section : Asm_section . t ; str : string ; comment : string option } include Identifiable . Make ( struct type nonrec t = t let compare { section = section1 ; str = str1 ; comment = comment1 } { section = section2 ; str = str2 ; comment = comment2 } = let c = Asm_section . compare section1 section2 in if c <> 0 then c else let c = String . compare str1 str2 in if c <> 0 then c else Option . compare String . compare comment1 comment2 let equal t1 t2 = compare t1 t2 = 0 let hash t = Hashtbl . hash t let print _ _ = Misc . fatal_error " Not yet implemented " let output _ _ = Misc . fatal_error " Not yet implemented " end ) end
module Make ( A : Asm_directives_intf . Arg ) : Asm_directives_intf . S = struct module Int8 = Numbers . Int8 module Int16 = Numbers . Int16 module Uint8 = Numbers . Uint8 module Uint16 = Numbers . Uint16 module Uint32 = Numbers . Uint32 module Uint64 = Numbers . Uint64 module D = A . D let sections_seen = ref [ ] let current_dwarf_section_ref = ref None let cached_strings = ref Cached_string . Map . empty let temp_var_counter = ref 0 let is_macos ( ) = match Target_system . assembler ( ) with | MASM | GAS_like -> false | MacOS -> true let is_gas ( ) = match Target_system . assembler ( ) with | MASM | MacOS -> false | GAS_like -> true let is_masm ( ) = match Target_system . assembler ( ) with | MASM -> true | GAS_like | MacOS -> false let loc ~ file_num ~ line ~ col = if not ( is_masm ( ) ) then D . loc ~ file_num ~ line ~ col ( ) let new_line ( ) = D . new_line ( ) let not_initialized ( ) = Misc . fatal_error " [ Asm_directives . initialize ] has not been called " let define_label label = let lbl_section = Asm_label . section label in let this_section = match ! current_dwarf_section_ref with | None -> not_initialized ( ) | Some this_section -> this_section in if not ( Asm_section . equal lbl_section this_section ) then Misc . fatal_errorf " Cannot define label % a intended for section % a in section % a " Asm_label . print label Asm_section . print lbl_section Asm_section . print this_section ; D . label ( Asm_label . encode label ) let switch_to_section section = let first_occurrence = if List . mem section ! sections_seen then false else begin sections_seen := section :: ! sections_seen ; true end in match ! current_dwarf_section_ref with | Some section ' when Asm_section . equal section section ' -> assert ( not first_occurrence ) ; ( ) | _ -> current_dwarf_section_ref := Some section ; let ( { names ; flags ; args } : Asm_section . section_details ) = Asm_section . details section ~ first_occurrence in if not first_occurrence then new_line ( ) ; D . section names flags args ; if first_occurrence then define_label ( Asm_label . for_section section ) let initialize ( ) = cached_strings := Cached_string . Map . empty ; sections_seen := [ ] ; temp_var_counter := 0 ; current_dwarf_section_ref := None ; if is_gas ( ) || is_macos ( ) then List . iter switch_to_section ( Asm_section . dwarf_sections_in_order ( ) ) ; let file_num = A . get_file_num " none " in loc ~ file_num ~ line : 1 ~ col : 1 ; D . text ( ) let with_comment f ? comment x = Option . iter D . comment comment ; f x let ( >> ) f g x = g ( f x ) let int8 = with_comment ( Int8 . to_int >> Int64 . of_int >> D . const_int64 >> D . byte ) let int16 = with_comment ( Int16 . to_int >> Int64 . of_int >> D . const_int64 >> D . word ) let int32 = with_comment ( Int64 . of_int32 >> D . const_int64 >> D . long ) let int64 = with_comment ( D . const_int64 >> D . qword ) let uint8 = with_comment ( Uint8 . to_int >> Int64 . of_int >> D . const_int64 >> D . byte ) let uint16 = with_comment ( Uint16 . to_int >> Int64 . of_int >> D . const_int64 >> D . word ) let uint32 = with_comment ( Uint32 . to_int64 >> D . const_int64 >> D . long ) let uint64 = with_comment ( Uint64 . to_int64 >> D . const_int64 >> D . qword ) let targetint ? comment num = match Targetint . repr num with | Int32 n -> int32 ? comment n | Int64 n -> int64 ? comment n let uleb128 = with_comment ( Uint64 . to_int64 >> D . const_int64 >> D . uleb128 ) let sleb128 = with_comment ( D . const_int64 >> D . sleb128 ) let assert_string_has_no_null_bytes s = assert ( not ( String . contains s ' \ x00 ' ) ) let string = with_comment ( fun str -> assert_string_has_no_null_bytes str ; D . bytes str ) let cache_string ? comment section str = assert_string_has_no_null_bytes str ; let cached : Cached_string . t = { section ; str ; comment } in match Cached_string . Map . find cached ! cached_strings with | label -> label | exception Not_found -> let label = Asm_label . create section in cached_strings := Cached_string . Map . add cached label ! cached_strings ; label let emit_cached_strings ( ) = let old_dwarf_section = ! current_dwarf_section_ref in Cached_string . Map . iter ( fun { section ; str ; comment } label_name -> switch_to_section section ; define_label label_name ; string ? comment str ; int8 Int8 . zero ) ! cached_strings ; cached_strings := Cached_string . Map . empty ; Option . iter switch_to_section old_dwarf_section let comment str = D . comment str let define_data_symbol symbol = let symbol = Asm_symbol . encode symbol in let data_type = D . QWORD in D . label ~ data_type symbol ; match Target_system . assembler ( ) , Target_system . is_windows ( ) with | GAS_like , false -> D . type_ symbol " STT_OBJECT " | GAS_like , true | MacOS , _ | MASM , _ -> ( ) let global sym = D . global ( Asm_symbol . encode sym ) let const_machine_width const = D . qword const let symbol = with_comment ( fun sym -> let lab = D . const_label ( Asm_symbol . encode sym ) in const_machine_width lab ) let label ? comment : _ _lab = A . emit_line " label " let symbol_plus_offset _sym ~ offset_in_bytes : _ = A . emit_line " symbol_plus_offset " let new_temp_var ( ) = let id = ! temp_var_counter in incr temp_var_counter ; Printf . sprintf " temp % d " id let force_assembly_time_constant expr = if not ( is_macos ( ) ) then expr else let temp = new_temp_var ( ) in D . direct_assignment temp expr ; D . const_label temp let between_symbols_in_current_unit ~ upper ~ lower = let upper = D . const_label ( Asm_symbol . encode upper ) in let lower = D . const_label ( Asm_symbol . encode lower ) in let expr = D . const_sub upper lower in if is_macos ( ) then const_machine_width ( force_assembly_time_constant expr ) else const_machine_width expr let between_labels_16_bit ? comment : _ ~ upper : _ ~ lower : _ ( ) = A . emit_line " between_labels_16_bit " let between_labels_32_bit ? comment : _ ~ upper : _ ~ lower : _ ( ) = A . emit_line " between_labels_32_bit " let between_labels_64_bit ? comment : _ ~ upper : _ ~ lower : _ ( ) = A . emit_line " between_labels_64_bit " let between_symbol_in_current_unit_and_label_offset ? comment : _ ~ upper : _ ~ lower : _ ~ offset_upper : _ ( ) = A . emit_line " between_symbol_in_current_unit_and_label_offset " let const ~ width constant = match width with | Dwarf_flags . Thirty_two -> D . long constant | Dwarf_flags . Sixty_four -> D . qword constant let offset_into_dwarf_section_label ? comment ~ width section upper = let upper_section = Asm_label . section upper in let expected_section : Asm_section . t = DWARF section in if not ( Asm_section . equal upper_section expected_section ) then Misc . fatal_errorf " Label % a ( in section % a ) is not in section % a " Asm_label . print upper Asm_section . print upper_section Asm_section . print expected_section ; ( if ! Clflags . keep_asm_file then let expected_section = Asm_section . to_string expected_section in match comment with | None -> D . comment ( Format . asprintf " offset into % s " expected_section ) | Some comment -> D . comment ( Format . asprintf " % s ( offset into % s ) " comment expected_section ) ) ; let expr = if is_macos ( ) then let lower = Asm_label . for_dwarf_section section in if Asm_label . equal lower upper then D . const_int64 0L else force_assembly_time_constant ( D . const_sub ( D . const_label ( Asm_label . encode upper ) ) ( D . const_label ( Asm_label . encode lower ) ) ) else D . const_label ( Asm_label . encode upper ) in const ~ width expr let offset_into_dwarf_section_symbol ? comment : _ ~ width : _ _section _symbol = A . emit_line " offset_into_dwarf_section_symbol " end
module type Arg = sig val emit_line : string -> unit val get_file_num : string -> int module D : sig type constant val const_int64 : int64 -> constant val const_label : string -> constant val const_add : constant -> constant -> constant val const_sub : constant -> constant -> constant type data_type = | NONE | DWORD | QWORD val file : file_num : int -> file_name : string -> unit val loc : file_num : int -> line : int -> col : int -> ? discriminator : int -> unit -> unit val comment : string -> unit val label : ? data_type : data_type -> string -> unit val section : string list -> string option -> string list -> unit val text : unit -> unit val new_line : unit -> unit val global : string -> unit val type_ : string -> string -> unit val byte : constant -> unit val word : constant -> unit val long : constant -> unit val qword : constant -> unit val bytes : string -> unit val uleb128 : constant -> unit val sleb128 : constant -> unit val direct_assignment : string -> constant -> unit end end
module type S = sig val switch_to_section : Asm_section . t -> unit val initialize : unit -> unit val int8 : ? comment : string -> Numbers . Int8 . t -> unit val int16 : ? comment : string -> Numbers . Int16 . t -> unit val int32 : ? comment : string -> Int32 . t -> unit val int64 : ? comment : string -> Int64 . t -> unit val uint8 : ? comment : string -> Numbers . Uint8 . t -> unit val uint16 : ? comment : string -> Numbers . Uint16 . t -> unit val uint32 : ? comment : string -> Numbers . Uint32 . t -> unit val uint64 : ? comment : string -> Numbers . Uint64 . t -> unit val targetint : ? comment : string -> Targetint . t -> unit val uleb128 : ? comment : string -> Numbers . Uint64 . t -> unit val sleb128 : ? comment : string -> Int64 . t -> unit val string : ? comment : string -> string -> unit val cache_string : ? comment : string -> Asm_section . t -> string -> Asm_label . t val emit_cached_strings : unit -> unit val comment : string -> unit val new_line : unit -> unit val define_data_symbol : Asm_symbol . t -> unit val global : Asm_symbol . t -> unit val symbol : ? comment : string -> Asm_symbol . t -> unit val define_label : Asm_label . t -> unit val label : ? comment : string -> Asm_label . t -> unit val symbol_plus_offset : Asm_symbol . t -> offset_in_bytes : Targetint . t -> unit val between_symbols_in_current_unit : upper : Asm_symbol . t -> lower : Asm_symbol . t -> unit val between_labels_16_bit : ? comment : string -> upper : Asm_label . t -> lower : Asm_label . t -> unit -> unit val between_labels_32_bit : ? comment : string -> upper : Asm_label . t -> lower : Asm_label . t -> unit -> unit val between_labels_64_bit : ? comment : string -> upper : Asm_label . t -> lower : Asm_label . t -> unit -> unit val between_symbol_in_current_unit_and_label_offset : ? comment : string -> upper : Asm_label . t -> lower : Asm_symbol . t -> offset_upper : Targetint . t -> unit -> unit val offset_into_dwarf_section_label : ? comment : string -> width : Dwarf_flags . dwarf_format -> Asm_section . dwarf_section -> Asm_label . t -> unit val offset_into_dwarf_section_symbol : ? comment : string -> width : Dwarf_flags . dwarf_format -> Asm_section . dwarf_section -> Asm_symbol . t -> unit end
type label = | Int of int | String of string
type t = { section : Asm_section . t ; label : label } type nonrec t = t let compare t1 t2 = Stdlib . compare t1 . label t2 . label let equal t1 t2 = compare t1 t2 = 0 let hash t = Hashtbl . hash t . label let print ppf t = match t . label with | Int i -> Format . fprintf ppf " L % d " i | String s -> Format . fprintf ppf " L % s " s let output _ _ = Misc . fatal_error " Not yet implemented " end )
let create_int section label = assert ( label >= 0 ) ; { section ; label = Int label }
let contains_escapable_char label = let found_escapable_char = ref false in String . iter ( fun c -> if Asm_symbol . should_be_escaped c then found_escapable_char := true ) label ; ! found_escapable_char
let create_string section label = assert ( not ( contains_escapable_char label ) ) ; { section ; label = String label }
let label_prefix = match Target_system . assembler ( ) with MacOS -> " L " | MASM | GAS_like -> " . L "
let encode ( t : t ) = match t . label with | Int label -> label_prefix ^ string_of_int label | String label -> label_prefix ^ label
let section t = t . section
let new_label_ref = ref ( fun _section -> Misc . fatal_error " [ Asm_label . initialize ] has not been called " )
let initialize ~ new_label = new_label_ref := fun section -> create_int section ( new_label ( ) )
let create ( section : Asm_section . t ) = ! new_label_ref section
let debug_info_label = lazy ( create ( DWARF Debug_info ) )
let debug_abbrev_label = lazy ( create ( DWARF Debug_abbrev ) )
let debug_aranges_label = lazy ( create ( DWARF Debug_aranges ) )
let debug_addr_label = lazy ( create ( DWARF Debug_addr ) )
let debug_loc_label = lazy ( create ( DWARF Debug_loc ) )
let debug_ranges_label = lazy ( create ( DWARF Debug_ranges ) )
let debug_loclists_label = lazy ( create ( DWARF Debug_loclists ) )
let debug_rnglists_label = lazy ( create ( DWARF Debug_rnglists ) )
let debug_str_label = lazy ( create ( DWARF Debug_str ) )
let debug_line_label = lazy ( create ( DWARF Debug_line ) )
let for_dwarf_section ( dwarf_section : Asm_section . dwarf_section ) = match dwarf_section with | Debug_info -> Lazy . force debug_info_label | Debug_abbrev -> Lazy . force debug_abbrev_label | Debug_aranges -> Lazy . force debug_aranges_label | Debug_addr -> Lazy . force debug_addr_label | Debug_loc -> Lazy . force debug_loc_label | Debug_ranges -> Lazy . force debug_ranges_label | Debug_loclists -> Lazy . force debug_loclists_label | Debug_rnglists -> Lazy . force debug_rnglists_label | Debug_str -> Lazy . force debug_str_label | Debug_line -> Lazy . force debug_line_label
let for_section ( section : Asm_section . t ) = match section with DWARF dwarf_section -> for_dwarf_section dwarf_section
type dwarf_section = | Debug_info | Debug_abbrev | Debug_aranges | Debug_addr | Debug_loc | Debug_ranges | Debug_loclists | Debug_rnglists | Debug_str | Debug_line
type t = DWARF of dwarf_section