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
|
|
|
(* From lambda to assembly code *)
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
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
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let liveness ppf phrase =
|
|
|
|
Liveness.fundecl ppf phrase; phrase
|
1995-08-13 02:31:50 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let dump_if ppf flag message phrase =
|
|
|
|
if !flag then Printmach.phase message ppf phrase
|
1998-11-12 08:53:20 -08:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let pass_dump_if ppf flag message phrase =
|
|
|
|
dump_if ppf flag message phrase; phrase
|
1995-08-13 02:31:50 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let pass_dump_linear_if ppf flag message phrase =
|
|
|
|
if !flag then fprintf ppf "*** %s@.%a@." message Printlinear.fundecl phrase;
|
1995-08-13 02:31:50 -07:00
|
|
|
phrase
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let rec regalloc ppf 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");
|
2000-04-21 01:13:22 -07:00
|
|
|
dump_if ppf dump_live "Liveness analysis" fd;
|
1995-07-02 09:41:48 -07:00
|
|
|
Interf.build_graph fd;
|
2000-04-21 01:13:22 -07:00
|
|
|
if !dump_interf then Printmach.interferences ppf ();
|
|
|
|
if !dump_prefer then Printmach.preferences ppf ();
|
1995-07-02 09:41:48 -07:00
|
|
|
Coloring.allocate_registers();
|
2000-04-21 01:13:22 -07:00
|
|
|
dump_if ppf dump_regalloc "After register allocation" fd;
|
1997-07-24 04:49:12 -07:00
|
|
|
let (newfd, redo_regalloc) = Reload.fundecl fd in
|
2000-04-21 01:13:22 -07:00
|
|
|
dump_if ppf dump_reload "After insertion of reloading code" newfd;
|
|
|
|
if redo_regalloc then begin
|
|
|
|
Reg.reinit(); Liveness.fundecl ppf newfd; regalloc ppf (round + 1) newfd
|
|
|
|
end else newfd
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-08-13 02:31:50 -07:00
|
|
|
let (++) x f = f x
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let compile_fundecl (ppf : formatter) fd_cmm =
|
1995-07-02 09:41:48 -07:00
|
|
|
Reg.reset();
|
1995-08-13 02:31:50 -07:00
|
|
|
fd_cmm
|
|
|
|
++ Selection.fundecl
|
2000-04-21 01:13:22 -07:00
|
|
|
++ pass_dump_if ppf dump_selection "After instruction selection"
|
1999-05-15 08:04:46 -07:00
|
|
|
++ Comballoc.fundecl
|
2000-04-21 01:13:22 -07:00
|
|
|
++ pass_dump_if ppf dump_combine "After allocation combining"
|
|
|
|
++ liveness ppf
|
|
|
|
++ pass_dump_if ppf dump_live "Liveness analysis"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Spill.fundecl
|
2000-04-21 01:13:22 -07:00
|
|
|
++ liveness ppf
|
|
|
|
++ pass_dump_if ppf dump_spill "After spilling"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Split.fundecl
|
2000-04-21 01:13:22 -07:00
|
|
|
++ pass_dump_if ppf dump_split "After live range splitting"
|
|
|
|
++ liveness ppf
|
|
|
|
++ regalloc ppf 1
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Linearize.fundecl
|
2000-04-21 01:13:22 -07:00
|
|
|
++ pass_dump_linear_if ppf dump_linear "Linearized code"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Scheduling.fundecl
|
2000-04-21 01:13:22 -07:00
|
|
|
++ pass_dump_linear_if ppf dump_scheduling "After instruction scheduling"
|
1995-08-13 02:31:50 -07:00
|
|
|
++ Emit.fundecl
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let compile_phrase ppf p =
|
|
|
|
if !dump_cmm then fprintf ppf "%a@." Printcmm.phrase p;
|
1995-07-02 09:41:48 -07:00
|
|
|
match p with
|
2000-04-21 01:13:22 -07:00
|
|
|
| Cfunction fd -> compile_fundecl ppf fd
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cdata dl -> Emit.data dl
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let compile_implementation prefixname ppf (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
|
2000-04-21 01:13:22 -07:00
|
|
|
++ List.iter (compile_phrase ppf) ++ (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 *)
|
|
|
|
|
2000-04-21 01:13:22 -07:00
|
|
|
let report_error ppf = function
|
|
|
|
| Assembler_error file ->
|
|
|
|
fprintf ppf "Assembler error, input left in file %s" file
|