Skip to content

Commit 88c2c8c

Browse files
committed
Also remove linking hack for bytecode
Adds the required_globals information to bytecode compilation units. This patch also bootstrap ocamlc. The cmo format is changed by this commit, there is no way around bootstraping here. Note that ocamldep and ocamllex does not rely on the cmo format, so they are not present in this commit. Changes in tests: * Update test/transprim/comparison_table.ml.reference: The (opaque (global List!)) expression is not present anymore * Update tests/no-alias-deps/aliases.cmo.reference The output of objinfo changed
1 parent a72d166 commit 88c2c8c

12 files changed

Lines changed: 413 additions & 403 deletions

File tree

boot/ocamlc

20.6 KB
Binary file not shown.

bytecomp/bytelink.ml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,11 +95,18 @@ let is_required (rel, _pos) =
9595
IdentSet.mem id !missing_globals
9696
| _ -> false
9797

98-
let add_required (rel, _pos) =
99-
match rel with
100-
Reloc_getglobal id ->
101-
missing_globals := IdentSet.add id !missing_globals
102-
| _ -> ()
98+
let add_required compunit =
99+
let add_required_by_reloc (rel, _pos) =
100+
match rel with
101+
Reloc_getglobal id ->
102+
missing_globals := IdentSet.add id !missing_globals
103+
| _ -> ()
104+
in
105+
let add_required_for_effects id =
106+
missing_globals := IdentSet.add id !missing_globals
107+
in
108+
List.iter add_required_by_reloc compunit.cu_reloc;
109+
List.iter add_required_for_effects compunit.cu_required_globals
103110

104111
let remove_required (rel, _pos) =
105112
match rel with
@@ -124,7 +131,7 @@ let scan_file obj_name tolink =
124131
seek_in ic compunit_pos;
125132
let compunit = (input_value ic : compilation_unit) in
126133
close_in ic;
127-
List.iter add_required compunit.cu_reloc;
134+
add_required compunit;
128135
Link_object(file_name, compunit) :: tolink
129136
end
130137
else if buffer = cma_magic_number then begin
@@ -143,7 +150,7 @@ let scan_file obj_name tolink =
143150
|| List.exists is_required compunit.cu_reloc
144151
then begin
145152
List.iter remove_required compunit.cu_reloc;
146-
List.iter add_required compunit.cu_reloc;
153+
add_required compunit;
147154
compunit :: reqd
148155
end else
149156
reqd)

bytecomp/bytepackager.ml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,6 +207,14 @@ let build_global_target oc target_name members mapping pos coercion =
207207
let package_object_files ppf files targetfile targetname coercion =
208208
let members =
209209
map_left_right read_member_info files in
210+
let required_globals =
211+
List.fold_left (fun required_globals -> function
212+
| { pm_kind = PM_intf } ->
213+
required_globals
214+
| { pm_kind = PM_impl { cu_required_globals } } ->
215+
List.fold_right Ident.Set.add cu_required_globals required_globals)
216+
Ident.Set.empty members
217+
in
210218
let unit_names =
211219
List.map (fun m -> m.pm_name) members in
212220
let mapping =
@@ -242,6 +250,7 @@ let package_object_files ppf files targetfile targetname coercion =
242250
cu_imports =
243251
(targetname, Some (Env.crc_of_unit targetname)) :: imports;
244252
cu_primitives = !primitives;
253+
cu_required_globals = Ident.Set.elements required_globals;
245254
cu_force_link = !force_link;
246255
cu_debug = if pos_final > pos_debug then pos_debug else 0;
247256
cu_debugsize = pos_final - pos_debug } in

bytecomp/cmo_format.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type compilation_unit =
3232
cu_reloc: (reloc_info * int) list; (* Relocation information *)
3333
cu_imports:
3434
(string * Digest.t option) list; (* Names and CRC of intfs imported *)
35+
cu_required_globals: Ident.t list; (* Compilation units whose initialization
36+
side effects must occur before this one. *)
3537
cu_primitives: string list; (* Primitives declared inside *)
3638
mutable cu_force_link: bool; (* Must be linked even if unref'ed *)
3739
mutable cu_debug: int; (* Position of debugging info, or 0 *)

bytecomp/emitcode.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ let rec emit = function
365365

366366
(* Emission to a file *)
367367

368-
let to_file outchan unit_name objfile code =
368+
let to_file outchan unit_name objfile ~required_globals code =
369369
init();
370370
output_string outchan cmo_magic_number;
371371
let pos_depl = pos_out outchan in
@@ -392,6 +392,7 @@ let to_file outchan unit_name objfile code =
392392
cu_imports = Env.imports();
393393
cu_primitives = List.map Primitive.byte_name
394394
!Translmod.primitive_declarations;
395+
cu_required_globals = Ident.Set.elements required_globals;
395396
cu_force_link = false;
396397
cu_debug = pos_debug;
397398
cu_debugsize = size_debug } in

bytecomp/emitcode.mli

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,14 @@
1818
open Cmo_format
1919
open Instruct
2020

21-
val to_file: out_channel -> string -> string -> instruction list -> unit
21+
val to_file: out_channel -> string -> string ->
22+
required_globals:Ident.Set.t -> instruction list -> unit
2223
(* Arguments:
2324
channel on output file
2425
name of compilation unit implemented
2526
path of cmo file being written
27+
required_globals: list of compilation units that must be
28+
evaluated before this one
2629
list of instructions to emit *)
2730
val to_memory: instruction list -> instruction list ->
2831
bytes * int * (reloc_info * int) list * debug_event list

bytecomp/translmod.ml

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -577,11 +577,6 @@ let required_globals ~flambda body =
577577
Hashtbl.clear used_primitives;
578578
required
579579

580-
let wrap_required_globals required body =
581-
Ident.Set.fold
582-
(fun id expr -> Lsequence(Lprim(Popaque, [Lprim(Pgetglobal id, [])]), expr))
583-
required body
584-
585580
(* Compile an implementation *)
586581

587582
let transl_implementation_flambda module_name (str, cc) =
@@ -602,9 +597,10 @@ let transl_implementation module_name (str, cc) =
602597
let implementation =
603598
transl_implementation_flambda module_name (str, cc)
604599
in
605-
Lprim (Psetglobal implementation.module_ident,
606-
[wrap_required_globals implementation.required_globals
607-
implementation.code])
600+
let code =
601+
Lprim (Psetglobal implementation.module_ident, [implementation.code])
602+
in
603+
{ implementation with code }
608604

609605
(* Build the list of value identifiers defined by a toplevel structure
610606
(excluding primitive declarations). *)

bytecomp/translmod.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
open Typedtree
2020
open Lambda
2121

22-
val transl_implementation: string -> structure * module_coercion -> lambda
22+
val transl_implementation: string -> structure * module_coercion -> Lambda.program
2323
val transl_store_phrases: string -> structure -> int * lambda
2424
val transl_store_implementation:
2525
string -> structure * module_coercion -> Lambda.program

driver/compile.ml

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,24 +80,25 @@ let implementation ppf sourcefile outputprefix =
8080
Warnings.check_fatal ();
8181
Stypes.dump (Some (outputprefix ^ ".annot"))
8282
end else begin
83-
let bytecode =
83+
let bytecode, required_globals =
8484
(typedtree, coercion)
8585
++ Timings.(time (Transl sourcefile))
8686
(Translmod.transl_implementation modulename)
87-
++ print_if ppf Clflags.dump_rawlambda Printlambda.lambda
8887
++ Timings.(accumulate_time (Generate sourcefile))
89-
(fun lambda ->
90-
Simplif.simplify_lambda lambda
88+
(fun { Lambda.code = lambda; required_globals } ->
89+
print_if ppf Clflags.dump_rawlambda Printlambda.lambda lambda
90+
++ Simplif.simplify_lambda
9191
++ print_if ppf Clflags.dump_lambda Printlambda.lambda
9292
++ Bytegen.compile_implementation modulename
93-
++ print_if ppf Clflags.dump_instr Printinstr.instrlist)
93+
++ print_if ppf Clflags.dump_instr Printinstr.instrlist
94+
++ fun bytecode -> bytecode, required_globals)
9495
in
9596
let objfile = outputprefix ^ ".cmo" in
9697
let oc = open_out_bin objfile in
9798
try
9899
bytecode
99100
++ Timings.(accumulate_time (Generate sourcefile))
100-
(Emitcode.to_file oc modulename objfile);
101+
(Emitcode.to_file oc modulename objfile ~required_globals);
101102
Warnings.check_fatal ();
102103
close_out oc;
103104
Stypes.dump (Some (outputprefix ^ ".annot"))

testsuite/tests/no-alias-deps/aliases.cmo.reference

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,8 @@ Interfaces imported:
88
-------------------------------- B
99
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa Aliases
1010
-------------------------------- A
11+
Required globals:
12+
D
13+
Pervasives
1114
Uses unsafe features: no
1215
Force link: no

0 commit comments

Comments
 (0)