Skip to content

Commit 8c52c4e

Browse files
authored
Compile refactor (#1096)
1 parent 986cec4 commit 8c52c4e

File tree

17 files changed

+228
-340
lines changed

17 files changed

+228
-340
lines changed

backend/asmgen.ml

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ let compile_unit ~output_prefix ~asm_filename ~keep_asm ~obj_filename ~may_reduc
431431
if create_asm && not keep_asm then remove_file asm_filename
432432
)
433433

434-
let end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile make_cmm =
434+
let end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile make_cmm =
435435
Emitaux.Dwarf_helpers.init ~disable_dwarf:false sourcefile;
436436
emit_begin_assembly unix;
437437
make_cmm ()
@@ -452,10 +452,6 @@ let end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile make_cmm =
452452
!Translmod.primitive_declarations));
453453
emit_end_assembly sourcefile ()
454454

455-
let end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile clambda =
456-
end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile (fun () ->
457-
Profile.record "cmm" Cmmgen.compunit clambda)
458-
459455
type middle_end =
460456
backend:(module Backend_intf.S)
461457
-> filename:string
@@ -464,43 +460,47 @@ type middle_end =
464460
-> Lambda.program
465461
-> Clambda.with_constants
466462

463+
type direct_to_cmm =
464+
ppf_dump:Format.formatter
465+
-> prefixname:string
466+
-> filename:string
467+
-> Lambda.program
468+
-> Cmm.phrase list
469+
470+
type pipeline =
471+
| Via_clambda of {
472+
backend : (module Backend_intf.S);
473+
middle_end : middle_end;
474+
}
475+
| Direct_to_cmm of direct_to_cmm
476+
467477
let asm_filename output_prefix =
468478
if !keep_asm_file || !Emitaux.binary_backend_available
469479
then output_prefix ^ ext_asm
470480
else Filename.temp_file "camlasm" ext_asm
471481

472-
let compile_implementation unix ?toplevel ~backend ~filename ~prefixname
473-
~middle_end ~ppf_dump (program : Lambda.program) =
482+
let compile_implementation unix ?toplevel ~pipeline
483+
~filename ~prefixname ~ppf_dump (program : Lambda.program) =
474484
compile_unit ~ppf_dump ~output_prefix:prefixname
475485
~asm_filename:(asm_filename prefixname) ~keep_asm:!keep_asm_file
476486
~obj_filename:(prefixname ^ ext_obj)
477487
~may_reduce_heap:(Option.is_none toplevel)
478488
(fun () ->
479489
Compilation_unit.Set.iter Compilenv.require_global
480490
program.required_globals;
481-
let clambda_with_constants =
482-
middle_end ~backend ~filename ~prefixname ~ppf_dump program
483-
in
484-
end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile:filename
485-
clambda_with_constants)
486-
487-
let compile_implementation_flambda2 unix ?toplevel ?(keep_symbol_tables=true)
488-
~filename ~prefixname ~size:module_block_size_in_words ~compilation_unit
489-
~module_initializer ~flambda2 ~ppf_dump ~required_globals () =
490-
compile_unit ~ppf_dump ~output_prefix:prefixname
491-
~asm_filename:(asm_filename prefixname) ~keep_asm:!keep_asm_file
492-
~obj_filename:(prefixname ^ ext_obj)
493-
~may_reduce_heap:(Option.is_none toplevel)
494-
(fun () ->
495-
Compilation_unit.Set.iter Compilenv.require_global
496-
required_globals;
497-
let cmm_phrases =
498-
flambda2 ~ppf_dump ~prefixname ~filename ~compilation_unit
499-
~module_block_size_in_words ~module_initializer
500-
~keep_symbol_tables
501-
in
502-
end_gen_implementation0 unix ?toplevel ~ppf_dump ~sourcefile:filename
503-
(fun () -> cmm_phrases))
491+
match pipeline with
492+
| Via_clambda { middle_end; backend; } ->
493+
let clambda_with_constants =
494+
middle_end ~backend ~filename ~prefixname ~ppf_dump program
495+
in
496+
end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile:filename
497+
(fun () -> Profile.record "cmm" Cmmgen.compunit clambda_with_constants)
498+
| Direct_to_cmm direct_to_cmm ->
499+
let cmm_phrases =
500+
direct_to_cmm ~ppf_dump ~prefixname ~filename program
501+
in
502+
end_gen_implementation unix ?toplevel ~ppf_dump ~sourcefile:filename
503+
(fun () -> cmm_phrases))
504504

505505
let linear_gen_implementation unix filename =
506506
let open Linear_format in

backend/asmgen.mli

Lines changed: 18 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,32 @@ type middle_end =
2424
-> Lambda.program
2525
-> Clambda.with_constants
2626

27-
(** Compile an implementation from Lambda using the given middle end. *)
28-
val compile_implementation
29-
: (module Compiler_owee.Unix_intf.S)
30-
-> ?toplevel:(string -> bool)
31-
-> backend:(module Backend_intf.S)
32-
-> filename:string
27+
(** The type of converters straight from Lambda to Cmm. This is how Flambda 2
28+
operates. *)
29+
type direct_to_cmm =
30+
ppf_dump:Format.formatter
3331
-> prefixname:string
34-
-> middle_end:middle_end
35-
-> ppf_dump:Format.formatter
32+
-> filename:string
3633
-> Lambda.program
37-
-> unit
34+
-> Cmm.phrase list
35+
36+
(** The ways to get from Lambda to Cmm. *)
37+
type pipeline =
38+
| Via_clambda of {
39+
backend : (module Backend_intf.S);
40+
middle_end : middle_end;
41+
}
42+
| Direct_to_cmm of direct_to_cmm
3843

39-
(** Compile an implementation from Lambda using Flambda 2.
40-
The Flambda 2 middle end neither uses the Clambda language nor the
41-
Cmmgen pass. Instead it emits Cmm directly. *)
42-
val compile_implementation_flambda2
44+
(** Compile an implementation from Lambda using the given middle end. *)
45+
val compile_implementation
4346
: (module Compiler_owee.Unix_intf.S)
4447
-> ?toplevel:(string -> bool)
45-
-> ?keep_symbol_tables:bool
48+
-> pipeline:pipeline
4649
-> filename:string
4750
-> prefixname:string
48-
-> size:int
49-
-> compilation_unit:Compilation_unit.t
50-
-> module_initializer:Lambda.lambda
51-
-> flambda2:(
52-
ppf_dump:Format.formatter ->
53-
prefixname:string ->
54-
filename:string ->
55-
compilation_unit:Compilation_unit.t ->
56-
module_block_size_in_words:int ->
57-
module_initializer:Lambda.lambda ->
58-
keep_symbol_tables:bool ->
59-
Cmm.phrase list)
6051
-> ppf_dump:Format.formatter
61-
-> required_globals:Compilation_unit.Set.t
62-
-> unit
52+
-> Lambda.program
6353
-> unit
6454

6555
val compile_implementation_linear

backend/asmpackager.ml

Lines changed: 41 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,16 @@ let check_units members =
8383

8484
(* Make the .o file for the package *)
8585

86+
type flambda2 =
87+
ppf_dump:Format.formatter ->
88+
prefixname:string ->
89+
filename:string ->
90+
keep_symbol_tables:bool ->
91+
Lambda.program ->
92+
Cmm.phrase list
93+
8694
let make_package_object unix ~ppf_dump members targetobj targetname coercion
87-
~backend ~flambda2 =
95+
~backend ~(flambda2 : flambda2) =
8896
Profile.record_call (Printf.sprintf "pack(%s)" targetname) (fun () ->
8997
let objtemp =
9098
if !Clflags.keep_asm_file
@@ -111,61 +119,38 @@ let make_package_object unix ~ppf_dump members targetobj targetname coercion
111119
let compilation_unit = CU.create for_pack_prefix modname in
112120
let prefixname = Filename.remove_extension objtemp in
113121
let required_globals = Compilation_unit.Set.empty in
114-
if Config.flambda2 then begin
115-
let main_module_block_size, module_initializer =
116-
Translmod.transl_package_flambda components coercion
117-
in
118-
let module_initializer = Simplif.simplify_lambda module_initializer in
119-
Asmgen.compile_implementation_flambda2 unix
120-
~filename:targetname
121-
~prefixname
122-
~size:main_module_block_size
123-
~compilation_unit
124-
~module_initializer
125-
~flambda2
126-
~ppf_dump
127-
~required_globals:required_globals
128-
~keep_symbol_tables:true
129-
()
130-
end else begin
131-
let program, middle_end =
132-
if Config.flambda then
133-
let main_module_block_size, code =
134-
Translmod.transl_package_flambda components coercion
135-
in
136-
let code = Simplif.simplify_lambda code in
137-
let program =
138-
{ Lambda.
139-
code;
140-
main_module_block_size;
141-
compilation_unit;
142-
required_globals;
143-
}
144-
in
145-
program, Flambda_middle_end.lambda_to_clambda
146-
else
147-
let main_module_block_size, code =
148-
Translmod.transl_store_package components
149-
compilation_unit coercion
150-
in
151-
let code = Simplif.simplify_lambda code in
152-
let program =
153-
{ Lambda.
154-
code;
155-
main_module_block_size;
156-
compilation_unit;
157-
required_globals;
158-
}
159-
in
160-
program, Closure_middle_end.lambda_to_clambda
161-
in
162-
Asmgen.compile_implementation ~backend unix
163-
~filename:targetname
164-
~prefixname
165-
~middle_end
166-
~ppf_dump
167-
program
168-
end;
122+
let transl_style : Translmod.compilation_unit_style =
123+
if Config.flambda || Config.flambda2 then Plain_block
124+
else Set_individual_fields
125+
in
126+
let main_module_block_size, code =
127+
Translmod.transl_package components compilation_unit coercion
128+
~style:transl_style
129+
in
130+
let code = Simplif.simplify_lambda code in
131+
let program =
132+
{ Lambda.
133+
code;
134+
main_module_block_size;
135+
compilation_unit;
136+
required_globals;
137+
}
138+
in
139+
let pipeline : Asmgen.pipeline =
140+
if Config.flambda2 then
141+
Direct_to_cmm (flambda2 ~keep_symbol_tables:true)
142+
else
143+
let middle_end =
144+
if Config.flambda then Flambda_middle_end.lambda_to_clambda
145+
else Closure_middle_end.lambda_to_clambda
146+
in
147+
Via_clambda { middle_end; backend }
148+
in
149+
Asmgen.compile_implementation ~pipeline unix
150+
~filename:targetname
151+
~prefixname
152+
~ppf_dump
153+
program;
169154
let objfiles =
170155
List.map
171156
(fun m -> Filename.remove_extension m.pm_file ^ Config.ext_obj)

backend/asmpackager.mli

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,8 @@ val package_files
2727
ppf_dump:Format.formatter ->
2828
prefixname:string ->
2929
filename:string ->
30-
compilation_unit:Compilation_unit.t ->
31-
module_block_size_in_words:int ->
32-
module_initializer:Lambda.lambda ->
3330
keep_symbol_tables:bool ->
31+
Lambda.program ->
3432
Cmm.phrase list)
3533
-> unit
3634

0 commit comments

Comments
 (0)