1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
|
|
|
(* under the terms of the Q Public License version 1.0. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Compilation environments for compilation units *)
|
|
|
|
|
|
|
|
open Config
|
|
|
|
open Misc
|
|
|
|
open Clambda
|
2010-05-19 04:29:38 -07:00
|
|
|
open Cmx_format
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
type error =
|
|
|
|
Not_a_unit_info of string
|
|
|
|
| Corrupted_unit_info of string
|
|
|
|
| Illegal_renaming of string * string
|
|
|
|
|
|
|
|
exception Error of error
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let global_infos_table =
|
|
|
|
(Hashtbl.create 17 : (string, unit_infos option) Hashtbl.t)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2011-03-29 00:58:53 -07:00
|
|
|
let structured_constants = ref ([] : (string * bool * Lambda.structured_constant) list)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let current_unit =
|
|
|
|
{ ui_name = "";
|
2005-08-01 08:51:09 -07:00
|
|
|
ui_symbol = "";
|
2002-02-08 08:55:44 -08:00
|
|
|
ui_defines = [];
|
1995-10-09 06:37:11 -07:00
|
|
|
ui_imports_cmi = [];
|
|
|
|
ui_imports_cmx = [];
|
1995-07-02 09:41:48 -07:00
|
|
|
ui_approx = Value_unknown;
|
|
|
|
ui_curry_fun = [];
|
1996-04-18 09:26:54 -07:00
|
|
|
ui_apply_fun = [];
|
2004-05-26 04:10:52 -07:00
|
|
|
ui_send_fun = [];
|
1996-04-18 09:26:54 -07:00
|
|
|
ui_force_link = false }
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let symbolname_for_pack pack name =
|
|
|
|
match pack with
|
|
|
|
| None -> name
|
|
|
|
| Some p ->
|
|
|
|
let b = Buffer.create 64 in
|
|
|
|
for i = 0 to String.length p - 1 do
|
|
|
|
match p.[i] with
|
|
|
|
| '.' -> Buffer.add_string b "__"
|
|
|
|
| c -> Buffer.add_char b c
|
|
|
|
done;
|
|
|
|
Buffer.add_string b "__";
|
|
|
|
Buffer.add_string b name;
|
|
|
|
Buffer.contents b
|
|
|
|
|
2011-03-29 00:58:53 -07:00
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let reset ?packname name =
|
|
|
|
Hashtbl.clear global_infos_table;
|
|
|
|
let symbol = symbolname_for_pack packname name in
|
1995-07-02 09:41:48 -07:00
|
|
|
current_unit.ui_name <- name;
|
2005-08-01 08:51:09 -07:00
|
|
|
current_unit.ui_symbol <- symbol;
|
|
|
|
current_unit.ui_defines <- [symbol];
|
1995-10-09 06:37:11 -07:00
|
|
|
current_unit.ui_imports_cmi <- [];
|
|
|
|
current_unit.ui_imports_cmx <- [];
|
1995-07-02 09:41:48 -07:00
|
|
|
current_unit.ui_curry_fun <- [];
|
1996-04-18 09:26:54 -07:00
|
|
|
current_unit.ui_apply_fun <- [];
|
2004-05-26 04:10:52 -07:00
|
|
|
current_unit.ui_send_fun <- [];
|
2011-03-29 00:58:53 -07:00
|
|
|
current_unit.ui_force_link <- false;
|
|
|
|
structured_constants := []
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let current_unit_infos () =
|
|
|
|
current_unit
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let current_unit_name () =
|
2004-01-05 12:26:19 -08:00
|
|
|
current_unit.ui_name
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let make_symbol ?(unitname = current_unit.ui_symbol) idopt =
|
2004-01-05 12:26:19 -08:00
|
|
|
let prefix = "caml" ^ unitname in
|
|
|
|
match idopt with
|
|
|
|
| None -> prefix
|
|
|
|
| Some id -> prefix ^ "__" ^ id
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-07-11 11:03:29 -07:00
|
|
|
let read_unit_info filename =
|
1995-07-02 09:41:48 -07:00
|
|
|
let ic = open_in_bin filename in
|
|
|
|
try
|
|
|
|
let buffer = String.create (String.length cmx_magic_number) in
|
|
|
|
really_input ic buffer 0 (String.length cmx_magic_number);
|
|
|
|
if buffer <> cmx_magic_number then begin
|
|
|
|
close_in ic;
|
|
|
|
raise(Error(Not_a_unit_info filename))
|
|
|
|
end;
|
|
|
|
let ui = (input_value ic : unit_infos) in
|
1995-10-09 06:37:11 -07:00
|
|
|
let crc = Digest.input ic in
|
1995-07-02 09:41:48 -07:00
|
|
|
close_in ic;
|
1995-07-11 11:03:29 -07:00
|
|
|
(ui, crc)
|
1995-07-02 09:41:48 -07:00
|
|
|
with End_of_file | Failure _ ->
|
|
|
|
close_in ic;
|
|
|
|
raise(Error(Corrupted_unit_info(filename)))
|
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
let read_library_info filename =
|
|
|
|
let ic = open_in_bin filename in
|
|
|
|
let buffer = String.create (String.length cmxa_magic_number) in
|
|
|
|
really_input ic buffer 0 (String.length cmxa_magic_number);
|
|
|
|
if buffer <> cmxa_magic_number then
|
|
|
|
raise(Error(Not_a_unit_info filename));
|
|
|
|
let infos = (input_value ic : library_infos) in
|
|
|
|
close_in ic;
|
|
|
|
infos
|
|
|
|
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
(* Read and cache info on global identifiers *)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-11-09 05:21:49 -08:00
|
|
|
let cmx_not_found_crc =
|
|
|
|
"\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000\000"
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let get_global_info global_ident =
|
1995-07-02 09:41:48 -07:00
|
|
|
let modname = Ident.name global_ident in
|
1999-02-04 02:31:16 -08:00
|
|
|
if modname = current_unit.ui_name then
|
2005-08-01 08:51:09 -07:00
|
|
|
Some current_unit
|
1999-02-04 02:31:16 -08:00
|
|
|
else begin
|
|
|
|
try
|
2005-08-01 08:51:09 -07:00
|
|
|
Hashtbl.find global_infos_table modname
|
1999-02-04 02:31:16 -08:00
|
|
|
with Not_found ->
|
2005-08-01 08:51:09 -07:00
|
|
|
let (infos, crc) =
|
1999-02-04 02:31:16 -08:00
|
|
|
try
|
|
|
|
let filename =
|
2002-06-07 00:35:38 -07:00
|
|
|
find_in_path_uncap !load_path (modname ^ ".cmx") in
|
1999-02-04 02:31:16 -08:00
|
|
|
let (ui, crc) = read_unit_info filename in
|
|
|
|
if ui.ui_name <> modname then
|
2000-04-21 01:13:22 -07:00
|
|
|
raise(Error(Illegal_renaming(ui.ui_name, filename)));
|
2005-08-01 08:51:09 -07:00
|
|
|
(Some ui, crc)
|
1999-02-04 02:31:16 -08:00
|
|
|
with Not_found ->
|
2005-08-01 08:51:09 -07:00
|
|
|
(None, cmx_not_found_crc) in
|
1999-02-04 02:31:16 -08:00
|
|
|
current_unit.ui_imports_cmx <-
|
|
|
|
(modname, crc) :: current_unit.ui_imports_cmx;
|
2005-08-01 08:51:09 -07:00
|
|
|
Hashtbl.add global_infos_table modname infos;
|
|
|
|
infos
|
|
|
|
end
|
|
|
|
|
2006-10-17 05:33:58 -07:00
|
|
|
let cache_unit_info ui =
|
|
|
|
Hashtbl.add global_infos_table ui.ui_name (Some ui)
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
(* Return the approximation of a global identifier *)
|
|
|
|
|
2007-11-06 07:16:56 -08:00
|
|
|
let toplevel_approx = Hashtbl.create 16
|
|
|
|
|
|
|
|
let record_global_approx_toplevel id =
|
|
|
|
Hashtbl.add toplevel_approx current_unit.ui_name current_unit.ui_approx
|
|
|
|
|
2005-08-01 08:51:09 -07:00
|
|
|
let global_approx id =
|
2007-11-06 07:16:56 -08:00
|
|
|
if Ident.is_predef_exn id then Value_unknown
|
|
|
|
else try Hashtbl.find toplevel_approx (Ident.name id)
|
2010-01-22 04:48:24 -08:00
|
|
|
with Not_found ->
|
2007-11-06 07:16:56 -08:00
|
|
|
match get_global_info id with
|
|
|
|
| None -> Value_unknown
|
|
|
|
| Some ui -> ui.ui_approx
|
2005-08-01 08:51:09 -07:00
|
|
|
|
|
|
|
(* Return the symbol used to refer to a global identifier *)
|
|
|
|
|
|
|
|
let symbol_for_global id =
|
|
|
|
if Ident.is_predef_exn id then
|
|
|
|
"caml_exn_" ^ Ident.name id
|
|
|
|
else begin
|
|
|
|
match get_global_info id with
|
|
|
|
| None -> make_symbol ~unitname:(Ident.name id) None
|
|
|
|
| Some ui -> make_symbol ~unitname:ui.ui_symbol None
|
1999-02-04 02:31:16 -08:00
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Register the approximation of the module being compiled *)
|
|
|
|
|
|
|
|
let set_global_approx approx =
|
|
|
|
current_unit.ui_approx <- approx
|
|
|
|
|
|
|
|
(* Record that a currying function or application function is needed *)
|
|
|
|
|
|
|
|
let need_curry_fun n =
|
|
|
|
if not (List.mem n current_unit.ui_curry_fun) then
|
|
|
|
current_unit.ui_curry_fun <- n :: current_unit.ui_curry_fun
|
|
|
|
|
|
|
|
let need_apply_fun n =
|
|
|
|
if not (List.mem n current_unit.ui_apply_fun) then
|
|
|
|
current_unit.ui_apply_fun <- n :: current_unit.ui_apply_fun
|
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
let need_send_fun n =
|
|
|
|
if not (List.mem n current_unit.ui_send_fun) then
|
|
|
|
current_unit.ui_send_fun <- n :: current_unit.ui_send_fun
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Write the description of the current unit *)
|
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
let write_unit_info info filename =
|
1995-07-02 09:41:48 -07:00
|
|
|
let oc = open_out_bin filename in
|
|
|
|
output_string oc cmx_magic_number;
|
2002-02-08 08:55:44 -08:00
|
|
|
output_value oc info;
|
1995-07-02 09:41:48 -07:00
|
|
|
flush oc;
|
1995-10-09 06:37:11 -07:00
|
|
|
let crc = Digest.file filename in
|
|
|
|
Digest.output oc crc;
|
1995-07-02 09:41:48 -07:00
|
|
|
close_out oc
|
|
|
|
|
2002-02-08 08:55:44 -08:00
|
|
|
let save_unit_info filename =
|
|
|
|
current_unit.ui_imports_cmi <- Env.imported_units();
|
|
|
|
write_unit_info current_unit filename
|
|
|
|
|
2011-03-29 00:58:53 -07:00
|
|
|
|
|
|
|
|
|
|
|
let const_label = ref 0
|
|
|
|
|
|
|
|
let new_const_label () =
|
|
|
|
incr const_label;
|
|
|
|
!const_label
|
|
|
|
|
|
|
|
let new_const_symbol () =
|
|
|
|
incr const_label;
|
|
|
|
make_symbol (Some (string_of_int !const_label))
|
|
|
|
|
|
|
|
let new_structured_constant cst global =
|
|
|
|
let lbl = new_const_symbol() in
|
|
|
|
structured_constants := (lbl, global, cst) :: !structured_constants;
|
|
|
|
lbl
|
|
|
|
|
|
|
|
let structured_constants () = !structured_constants
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Error report *)
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
open Format
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let report_error ppf = function
|
|
|
|
| Not_a_unit_info filename ->
|
|
|
|
fprintf ppf "%s@ is not a compilation unit description." filename
|
1995-07-02 09:41:48 -07:00
|
|
|
| Corrupted_unit_info filename ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "Corrupted compilation unit description@ %s" filename
|
1995-07-02 09:41:48 -07:00
|
|
|
| Illegal_renaming(modname, filename) ->
|
2000-04-21 01:13:22 -07:00
|
|
|
fprintf ppf "%s@ contains the description for unit@ %s" filename modname
|