-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathcompilenv.ml
393 lines (335 loc) · 13.2 KB
/
compilenv.ml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
(**************************************************************************)
(* *)
(* OCaml *)
(* *)
(* Xavier Leroy, projet Gallium, INRIA Rocquencourt *)
(* Pierre Chambart, OCamlPro *)
(* Mark Shinwell and Leo White, Jane Street Europe *)
(* *)
(* Copyright 2010 Institut National de Recherche en Informatique et *)
(* en Automatique *)
(* Copyright 2013--2016 OCamlPro SAS *)
(* Copyright 2014--2016 Jane Street Group LLC *)
(* *)
(* All rights reserved. This file is distributed under the terms of *)
(* the GNU Lesser General Public License version 2.1, with the *)
(* special exception on linking described in the file LICENSE. *)
(* *)
(**************************************************************************)
(* Compilation environments for compilation units *)
[@@@ocaml.warning "+a-4-9-40-41-42"]
open Config
open Cmx_format
module CU = Compilation_unit
type error =
Not_a_unit_info of string
| Corrupted_unit_info of string
| Illegal_renaming of CU.t * CU.t * string
| Mismatching_for_pack of
string * CU.Prefix.t * CU.Name.t * CU.Prefix.t option
exception Error of error
let global_infos_table =
(CU.Name.Tbl.create 17 : unit_infos option CU.Name.Tbl.t)
let export_infos_table =
(CU.Name.Tbl.create 10 : Export_info.t CU.Name.Tbl.t)
let imported_sets_of_closures_table =
(Set_of_closures_id.Tbl.create 10
: Simple_value_approx.function_declarations option
Set_of_closures_id.Tbl.t)
module CstMap =
Map.Make(struct
type t = Clambda.ustructured_constant
let compare = Clambda.compare_structured_constants
(* PR#6442: it is incorrect to use Stdlib.compare on values of type t
because it compares "0.0" and "-0.0" equal. *)
end)
module SymMap = Misc.Stdlib.String.Map
type structured_constants =
{
strcst_shared: string CstMap.t;
strcst_all: Clambda.ustructured_constant SymMap.t;
}
let structured_constants_empty =
{
strcst_shared = CstMap.empty;
strcst_all = SymMap.empty;
}
let structured_constants = ref structured_constants_empty
let exported_constants = Hashtbl.create 17
let merged_environment = ref Export_info.empty
let default_ui_export_info =
if Config.flambda then
Cmx_format.Flambda Export_info.empty
else
Cmx_format.Clambda Value_unknown
let current_unit =
{ ui_unit = CU.dummy;
ui_defines = [];
ui_imports_cmi = [| |];
ui_imports_cmx = [| |];
ui_curry_fun = [];
ui_apply_fun = [];
ui_send_fun = [];
ui_force_link = false;
ui_export_info = default_ui_export_info;
ui_for_pack = None }
let reset compilation_unit =
CU.Name.Tbl.clear global_infos_table;
Set_of_closures_id.Tbl.clear imported_sets_of_closures_table;
CU.set_current (Some compilation_unit);
current_unit.ui_unit <- compilation_unit;
current_unit.ui_defines <- [compilation_unit];
current_unit.ui_imports_cmi <- [| |];
current_unit.ui_imports_cmx <- [| |];
current_unit.ui_curry_fun <- [];
current_unit.ui_apply_fun <- [];
current_unit.ui_send_fun <- [];
current_unit.ui_force_link <- !Clflags.link_everything;
Hashtbl.clear exported_constants;
structured_constants := structured_constants_empty;
current_unit.ui_export_info <- default_ui_export_info;
merged_environment := Export_info.empty;
CU.Name.Tbl.clear export_infos_table
let current_unit_infos () =
current_unit
let read_unit_info filename =
let ic = open_in_bin filename in
try
let buffer = really_input_string ic (String.length cmx_magic_number) in
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
let crc = Digest.input ic in
close_in ic;
(ui, crc)
with End_of_file | Failure _ ->
close_in ic;
raise(Error(Corrupted_unit_info(filename)))
let read_library_info filename =
let ic = open_in_bin filename in
let buffer = really_input_string ic (String.length cmxa_magic_number) in
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
(* Read and cache info on global identifiers *)
let get_unit_info comp_unit =
(* If this fails, it likely means that someone didn't call
[CU.which_cmx_file]. *)
assert (CU.can_access_cmx_file comp_unit ~accessed_by:current_unit.ui_unit);
(* CR lmaurer: Surely this should just compare [comp_unit] to
[current_unit.ui_unit], but doing so seems to break Closure. We should fix
that. *)
if CU.Name.equal (CU.name comp_unit) (CU.name current_unit.ui_unit)
then
Some current_unit
else begin
let cmx_name = CU.name comp_unit in
try
CU.Name.Tbl.find global_infos_table cmx_name
with Not_found ->
let (infos, crc) =
if Env.is_imported_opaque cmx_name then (None, None)
else begin
try
let filename =
Load_path.find_uncap ((cmx_name |> CU.Name.to_string) ^ ".cmx") in
let (ui, crc) = read_unit_info filename in
if not (CU.equal ui.ui_unit comp_unit) then
raise(Error(Illegal_renaming(comp_unit, ui.ui_unit, filename)));
(* Linking to a compilation unit expected to go into a
pack is possible only from
inside the same pack, but it is perfectly ok to link to
an unit outside of the pack. *)
let[@inline] for_pack_prefix unit =
let prefix = CU.for_pack_prefix unit in
if CU.Prefix.is_empty prefix then None else Some prefix
in
(match for_pack_prefix ui.ui_unit,
for_pack_prefix current_unit.ui_unit
with
| None, _ -> ()
| Some p1, Some p2 when CU.Prefix.equal p1 p2 -> ()
| Some p1, p2 ->
raise (Error (Mismatching_for_pack
(filename, p1, CU.name current_unit.ui_unit, p2))));
(Some ui, Some crc)
with Not_found ->
let warn = Warnings.No_cmx_file (cmx_name |> CU.Name.to_string) in
Location.prerr_warning Location.none warn;
(None, None)
end
in
let import = Import_info.create_normal comp_unit ~crc in
current_unit.ui_imports_cmx <-
Array.append [| import |] current_unit.ui_imports_cmx;
CU.Name.Tbl.add global_infos_table cmx_name infos;
infos
end
let which_cmx_file desired_comp_unit =
CU.which_cmx_file desired_comp_unit ~accessed_by:(CU.get_current_exn ())
let get_global_info global_ident =
get_unit_info (which_cmx_file global_ident)
let cache_unit_info ui =
CU.Name.Tbl.add global_infos_table (CU.name ui.ui_unit) (Some ui)
(* Return the approximation of a global identifier *)
let get_clambda_approx ui =
assert(not Config.flambda);
match ui.ui_export_info with
| Flambda _ -> assert false
| Clambda approx -> approx
let toplevel_approx :
(CU.t, Clambda.value_approximation) Hashtbl.t = Hashtbl.create 16
let record_global_approx_toplevel () =
Hashtbl.add toplevel_approx
current_unit.ui_unit
(get_clambda_approx current_unit)
let global_approx id =
try Hashtbl.find toplevel_approx id
with Not_found ->
match get_global_info id with
| None -> Clambda.Value_unknown
| Some ui -> get_clambda_approx ui
(* Register the approximation of the module being compiled *)
let set_global_approx approx =
assert(not Config.flambda);
current_unit.ui_export_info <- Clambda approx
(* Exporting and importing cross module information (Flambda only) *)
let get_flambda_export_info ui =
assert(Config.flambda);
match ui.ui_export_info with
| Clambda _ -> assert false
| Flambda ei -> ei
let set_export_info export_info =
assert(Config.flambda);
current_unit.ui_export_info <- Flambda export_info
let approx_for_global comp_unit =
if CU.equal comp_unit CU.predef_exn
then invalid_arg "approx_for_global with predef_exn compilation unit";
let accessible_comp_unit = which_cmx_file comp_unit in
let cmx_name = CU.name accessible_comp_unit in
match CU.Name.Tbl.find export_infos_table cmx_name with
| otherwise -> Some otherwise
| exception Not_found ->
match get_unit_info accessible_comp_unit with
| None -> None
| Some ui ->
let exported = get_flambda_export_info ui in
CU.Name.Tbl.add export_infos_table cmx_name exported;
merged_environment := Export_info.merge !merged_environment exported;
Some exported
let approx_env () = !merged_environment
(* Record that a currying function or application function is needed *)
let need_curry_fun kind arity result =
if not (List.mem (kind, arity, result) current_unit.ui_curry_fun) then
current_unit.ui_curry_fun <-
(kind, arity, result) :: current_unit.ui_curry_fun
let need_apply_fun arity result mode =
assert(List.compare_length_with arity 0 > 0);
if not (List.mem (arity, result, mode) current_unit.ui_apply_fun) then
current_unit.ui_apply_fun <-
(arity, result, mode) :: current_unit.ui_apply_fun
let need_send_fun arity result mode =
if not (List.mem (arity, result, mode) current_unit.ui_send_fun) then
current_unit.ui_send_fun <-
(arity, result, mode) :: current_unit.ui_send_fun
(* Write the description of the current unit *)
let write_unit_info info filename =
let oc = open_out_bin filename in
output_string oc cmx_magic_number;
output_value oc info;
flush oc;
let crc = Digest.file filename in
Digest.output oc crc;
close_out oc
let save_unit_info filename =
current_unit.ui_imports_cmi <- Array.of_list (Env.imports());
write_unit_info current_unit filename
let snapshot () = !structured_constants
let backtrack s = structured_constants := s
let new_const_symbol () =
Symbol.for_new_const_in_current_unit ()
|> Symbol.linkage_name
|> Linkage_name.to_string
let new_structured_constant cst ~shared =
let {strcst_shared; strcst_all} = !structured_constants in
if shared then
try
CstMap.find cst strcst_shared
with Not_found ->
let lbl = new_const_symbol() in
structured_constants :=
{
strcst_shared = CstMap.add cst lbl strcst_shared;
strcst_all = SymMap.add lbl cst strcst_all;
};
lbl
else
let lbl = new_const_symbol() in
structured_constants :=
{
strcst_shared;
strcst_all = SymMap.add lbl cst strcst_all;
};
lbl
let add_exported_constant s =
Hashtbl.replace exported_constants s ()
let clear_structured_constants () =
structured_constants := structured_constants_empty
let structured_constant_of_symbol s =
SymMap.find_opt s (!structured_constants).strcst_all
let structured_constants () =
let provenance : Clambda.usymbol_provenance =
{ original_idents = [];
module_path =
(* CR-someday lmaurer: Properly construct a [Path.t] from the module name
with its pack prefix. *)
Path.Pident (Ident.create_persistent (Compilation_unit.Name.to_string (
Compilation_unit.name (Compilation_unit.get_current_exn ()))));
}
in
SymMap.bindings (!structured_constants).strcst_all
|> List.map
(fun (symbol, definition) ->
{
Clambda.symbol;
exported = Hashtbl.mem exported_constants symbol;
definition;
provenance = Some provenance;
})
let require_global global_ident =
ignore (get_global_info global_ident : Cmx_format.unit_infos option)
(* Error report *)
open Format
let report_error ppf = function
| Not_a_unit_info filename ->
fprintf ppf "%a@ is not a compilation unit description."
Location.print_filename filename
| Corrupted_unit_info filename ->
fprintf ppf "Corrupted compilation unit description@ %a"
Location.print_filename filename
| Illegal_renaming(name, modname, filename) ->
fprintf ppf "%a@ contains the description for unit\
@ %a when %a was expected"
Location.print_filename filename
CU.print name
CU.print modname
| Mismatching_for_pack(filename, pack_1, current_unit, None) ->
fprintf ppf "%a@ was built with -for-pack %a, but the \
@ current unit %a is not"
Location.print_filename filename CU.Prefix.print pack_1
CU.Name.print current_unit
| Mismatching_for_pack(filename, pack_1, current_unit, Some pack_2) ->
fprintf ppf "%a@ was built with -for-pack %a, but the \
@ current unit %a is built with -for-pack %a"
Location.print_filename filename CU.Prefix.print pack_1
CU.Name.print current_unit CU.Prefix.print pack_2
let () =
Location.register_error_of_exn
(function
| Error err -> Some (Location.error_of_printer_file report_error err)
| _ -> None
)