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 *)
|
1998-11-12 08:53:20 -08:00
|
|
|
(* en Automatique. Distributed only by permission. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* From lambda to assembly code *)
|
|
|
|
|
|
|
|
open Format
|
1996-02-15 08:19:09 -08:00
|
|
|
open Config
|
1995-07-02 09:41:48 -07:00
|
|
|
open Clflags
|
|
|
|
open Misc
|
|
|
|
open Cmm
|
|
|
|
|
|
|
|
type error = Assembler_error of string
|
|
|
|
|
|
|
|
exception Error of error
|
|
|
|
|
1995-08-13 02:31:50 -07:00
|
|
|
let liveness phrase =
|
|
|
|
Liveness.fundecl phrase; phrase
|
|
|
|
|
|
|
|
let dump_if flag message phrase =
|
1998-11-12 08:53:20 -08:00
|
|
|
if !flag then Printmach.phase message phrase
|
|
|
|
|
|
|
|
let pass_dump_if flag message phrase =
|
|
|
|
dump_if flag message phrase; phrase
|
1995-08-13 02:31:50 -07:00
|
|
|
|
1998-11-12 08:53:20 -08:00
|
|
|
let pass_dump_linear_if flag message phrase =
|
1995-08-13 02:31:50 -07:00
|
|
|
if !flag then begin
|
|
|
|
print_string "*** "; print_string message; print_newline();
|
|
|
|
Printlinear.fundecl phrase; print_newline()
|
|
|
|
end;
|
|
|
|
phrase
|
|
|
|
|
1996-09-11 08:59:11 -07:00
|
|
|
let rec regalloc round fd =
|
1996-09-26 10:13:34 -07:00
|
|
|
if round > 50 then
|
1996-09-11 08:59:11 -07:00
|
|
|
fatal_error(fd.Mach.fun_name ^
|
|
|
|
": function too complex, cannot complete register allocation");
|
1995-08-13 02:31:50 -07:00
|
|
|
dump_if dump_live "Liveness analysis" fd;
|
1995-07-02 09:41:48 -07:00
|
|
|
Interf.build_graph fd;
|
|
|
|
if !dump_interf then Printmach.interferences();
|
|
|
|
if !dump_prefer then Printmach.preferences();
|
|
|
|
Coloring.allocate_registers();
|
1995-08-13 02:31:50 -07:00
|
|
|
dump_if dump_regalloc "After register allocation" fd;
|
1997-07-24 04:49:12 -07:00
|
|
|
let (newfd, redo_regalloc) = Reload.fundecl fd in
|
1995-08-13 02:31:50 -07:00
|
|
|
dump_if dump_reload "After insertion of reloading code" newfd;
|
1995-07-02 09:41:48 -07:00
|
|
|
if redo_regalloc
|
1996-09-11 08:59:11 -07:00
|
|
|
then begin Reg.reinit(); Liveness.fundecl newfd; regalloc (round+1) newfd end
|
1995-07-02 09:41:48 -07:00
|
|
|
else newfd
|
|
|
|
|
1995-08-13 02:31:50 -07:00
|
|
|
let (++) x f = f x
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let compile_fundecl fd_cmm =
|
|
|
|
Reg.reset();
|
1995-08-13 02:31:50 -07:00
|
|
|
fd_cmm
|
|
|
|
++ Selection.fundecl
|
1998-11-12 08:53:20 -08:00
|
|
|
++ pass_dump_if dump_selection "After instruction selection"
|
1999-05-15 08:04:46 -07:00
|
|
|
++ Comballoc.fundecl
|
|
|
|
++ pass_dump_if dump_combine "After allocation combining"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ liveness
|
1998-11-12 08:53:20 -08:00
|
|
|
++ pass_dump_if dump_live "Liveness analysis"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Spill.fundecl
|
|
|
|
++ liveness
|
1998-11-12 08:53:20 -08:00
|
|
|
++ pass_dump_if dump_spill "After spilling"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Split.fundecl
|
1998-11-12 08:53:20 -08:00
|
|
|
++ pass_dump_if dump_split "After live range splitting"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ liveness
|
1996-09-11 08:59:11 -07:00
|
|
|
++ regalloc 1
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Linearize.fundecl
|
1998-11-12 08:53:20 -08:00
|
|
|
++ pass_dump_linear_if dump_linear "Linearized code"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Scheduling.fundecl
|
1998-11-12 08:53:20 -08:00
|
|
|
++ pass_dump_linear_if dump_scheduling "After instruction scheduling"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Emit.fundecl
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
let compile_phrase p =
|
|
|
|
if !dump_cmm then begin Printcmm.phrase p; print_newline() end;
|
|
|
|
match p with
|
|
|
|
Cfunction fd -> compile_fundecl fd
|
|
|
|
| Cdata dl -> Emit.data dl
|
|
|
|
|
1998-04-27 08:16:48 -07:00
|
|
|
let compile_implementation prefixname (size, lam) =
|
1995-07-02 09:41:48 -07:00
|
|
|
let asmfile =
|
1996-01-04 04:49:06 -08:00
|
|
|
if !keep_asm_file
|
1996-02-15 08:19:09 -08:00
|
|
|
then prefixname ^ ext_asm
|
|
|
|
else Filename.temp_file "camlasm" ext_asm in
|
1995-07-02 09:41:48 -07:00
|
|
|
let oc = open_out asmfile in
|
|
|
|
begin try
|
|
|
|
Emitaux.output_channel := oc;
|
|
|
|
Emit.begin_assembly();
|
1998-04-27 08:16:48 -07:00
|
|
|
Closure.intro size lam
|
|
|
|
++ Cmmgen.compunit size
|
|
|
|
++ List.iter compile_phrase ++ (fun () -> ());
|
1995-07-02 09:41:48 -07:00
|
|
|
Emit.end_assembly();
|
|
|
|
close_out oc
|
|
|
|
with x ->
|
1995-07-07 05:07:07 -07:00
|
|
|
close_out oc;
|
1995-07-20 00:51:11 -07:00
|
|
|
if !keep_asm_file then () else remove_file asmfile;
|
1995-07-07 05:07:07 -07:00
|
|
|
raise x
|
1995-07-02 09:41:48 -07:00
|
|
|
end;
|
1996-02-15 08:19:09 -08:00
|
|
|
if Proc.assemble_file asmfile (prefixname ^ ext_obj) <> 0
|
1995-07-20 00:51:11 -07:00
|
|
|
then raise(Error(Assembler_error asmfile));
|
|
|
|
if !keep_asm_file then () else remove_file asmfile
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Error report *)
|
|
|
|
|
|
|
|
let report_error = function
|
|
|
|
Assembler_error file ->
|
|
|
|
print_string "Assembler error, input left in file ";
|
|
|
|
print_string file
|