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-06-15 01:17:29 -07:00
|
|
|
(* Transformation of Mach code into a list of pseudo-instructions. *)
|
|
|
|
|
1995-07-24 05:44:17 -07:00
|
|
|
open Reg
|
1995-06-15 01:17:29 -07:00
|
|
|
open Mach
|
|
|
|
|
|
|
|
type label = int
|
|
|
|
|
|
|
|
let label_counter = ref 99
|
|
|
|
|
|
|
|
let new_label() = incr label_counter; !label_counter
|
|
|
|
|
|
|
|
type instruction =
|
1997-03-07 07:32:26 -08:00
|
|
|
{ mutable desc: instruction_desc;
|
2002-01-09 11:40:48 -08:00
|
|
|
mutable next: instruction;
|
1995-06-15 01:17:29 -07:00
|
|
|
arg: Reg.t array;
|
|
|
|
res: Reg.t array;
|
2007-01-29 04:11:18 -08:00
|
|
|
dbg: Debuginfo.t;
|
1995-06-15 01:17:29 -07:00
|
|
|
live: Reg.Set.t }
|
|
|
|
|
|
|
|
and instruction_desc =
|
|
|
|
Lend
|
|
|
|
| Lop of operation
|
1995-08-25 01:46:03 -07:00
|
|
|
| Lreloadretaddr
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lreturn
|
|
|
|
| Llabel of label
|
|
|
|
| Lbranch of label
|
|
|
|
| Lcondbranch of test * label
|
1995-08-12 07:26:23 -07:00
|
|
|
| Lcondbranch3 of label option * label option * label option
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lswitch of label array
|
1995-07-07 05:07:07 -07:00
|
|
|
| Lsetuptrap of label
|
|
|
|
| Lpushtrap
|
1995-06-15 01:17:29 -07:00
|
|
|
| Lpoptrap
|
|
|
|
| Lraise
|
|
|
|
|
2001-02-05 00:49:10 -08:00
|
|
|
let has_fallthrough = function
|
2000-10-02 07:08:30 -07:00
|
|
|
| Lreturn | Lbranch _ | Lswitch _ | Lraise
|
|
|
|
| Lop Itailcall_ind | Lop (Itailcall_imm _) -> false
|
|
|
|
| _ -> true
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
type fundecl =
|
|
|
|
{ fun_name: string;
|
1995-07-02 09:41:48 -07:00
|
|
|
fun_body: instruction;
|
|
|
|
fun_fast: bool }
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* Invert a test *)
|
|
|
|
|
|
|
|
let invert_integer_test = function
|
|
|
|
Isigned cmp -> Isigned(Cmm.negate_comparison cmp)
|
|
|
|
| Iunsigned cmp -> Iunsigned(Cmm.negate_comparison cmp)
|
|
|
|
|
|
|
|
let invert_test = function
|
1995-06-15 09:08:53 -07:00
|
|
|
Itruetest -> Ifalsetest
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ifalsetest -> Itruetest
|
|
|
|
| Iinttest(cmp) -> Iinttest(invert_integer_test cmp)
|
|
|
|
| Iinttest_imm(cmp, n) -> Iinttest_imm(invert_integer_test cmp, n)
|
1995-11-28 06:25:57 -08:00
|
|
|
| Ifloattest(cmp, neg) -> Ifloattest(cmp, not neg)
|
1995-07-17 09:10:15 -07:00
|
|
|
| Ieventest -> Ioddtest
|
|
|
|
| Ioddtest -> Ieventest
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* The "end" instruction *)
|
|
|
|
|
|
|
|
let rec end_instr =
|
|
|
|
{ desc = Lend;
|
|
|
|
next = end_instr;
|
|
|
|
arg = [||];
|
|
|
|
res = [||];
|
2007-01-29 04:11:18 -08:00
|
|
|
dbg = Debuginfo.none;
|
1995-06-15 01:17:29 -07:00
|
|
|
live = Reg.Set.empty }
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
(* Cons an instruction (live, debug empty) *)
|
1995-08-13 02:31:50 -07:00
|
|
|
|
|
|
|
let instr_cons d a r n =
|
2007-01-29 04:11:18 -08:00
|
|
|
{ desc = d; next = n; arg = a; res = r;
|
|
|
|
dbg = Debuginfo.none; live = Reg.Set.empty }
|
1995-08-13 02:31:50 -07:00
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Cons a simple instruction (arg, res, live empty) *)
|
|
|
|
|
|
|
|
let cons_instr d n =
|
2007-01-29 04:11:18 -08:00
|
|
|
{ desc = d; next = n; arg = [||]; res = [||];
|
|
|
|
dbg = Debuginfo.none; live = Reg.Set.empty }
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
(* Build an instruction with arg, res, dbg, live taken from
|
1995-07-19 02:56:51 -07:00
|
|
|
the given Mach.instruction *)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let copy_instr d i n =
|
|
|
|
{ desc = d; next = n;
|
2007-01-29 04:11:18 -08:00
|
|
|
arg = i.Mach.arg; res = i.Mach.res;
|
|
|
|
dbg = i.Mach.dbg; live = i.Mach.live }
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
(*
|
|
|
|
Label the beginning of the given instruction sequence.
|
|
|
|
- If the sequence starts with a branch, jump over it.
|
|
|
|
- If the sequence is the end, (tail call position), just do nothing
|
|
|
|
*)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
let get_label n = match n.desc with
|
1995-06-15 01:17:29 -07:00
|
|
|
Lbranch lbl -> (lbl, n)
|
|
|
|
| Llabel lbl -> (lbl, n)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Lend -> (-1, n)
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> let lbl = new_label() in (lbl, cons_instr (Llabel lbl) n)
|
|
|
|
|
2001-02-05 00:49:10 -08:00
|
|
|
(* Check the fallthrough label *)
|
2000-10-02 07:08:30 -07:00
|
|
|
let check_label n = match n.desc with
|
|
|
|
| Lbranch lbl -> lbl
|
|
|
|
| Llabel lbl -> lbl
|
|
|
|
| _ -> -1
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Discard all instructions up to the next label.
|
2000-01-07 07:50:42 -08:00
|
|
|
This function is to be called before adding a non-terminating
|
1995-06-15 01:17:29 -07:00
|
|
|
instruction. *)
|
|
|
|
|
|
|
|
let rec discard_dead_code n =
|
|
|
|
match n.desc with
|
|
|
|
Lend -> n
|
|
|
|
| Llabel _ -> n
|
1995-11-16 08:41:18 -08:00
|
|
|
(* Do not discard Lpoptrap or Istackoffset instructions,
|
|
|
|
as this may cause a stack imbalance later during assembler generation. *)
|
|
|
|
| Lpoptrap -> n
|
|
|
|
| Lop(Istackoffset _) -> n
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> discard_dead_code n.next
|
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
(*
|
|
|
|
Add a branch in front of a continuation.
|
1995-06-15 01:17:29 -07:00
|
|
|
Discard dead code in the continuation.
|
2000-10-02 07:08:30 -07:00
|
|
|
Does not insert anything if we're just falling through
|
|
|
|
or if we jump to dead code after the end of function (lbl=-1)
|
|
|
|
*)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let add_branch lbl n =
|
2000-10-02 07:08:30 -07:00
|
|
|
if lbl >= 0 then
|
|
|
|
let n1 = discard_dead_code n in
|
|
|
|
match n1.desc with
|
|
|
|
| Llabel lbl1 when lbl1 = lbl -> n1
|
|
|
|
| _ -> cons_instr (Lbranch lbl) n1
|
|
|
|
else
|
|
|
|
discard_dead_code n
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-08-11 12:50:59 -07:00
|
|
|
(* Current labels for exit handler *)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-08-11 12:50:59 -07:00
|
|
|
let exit_label = ref []
|
|
|
|
|
|
|
|
let find_exit_label k =
|
|
|
|
try
|
|
|
|
List.assoc k !exit_label
|
|
|
|
with
|
|
|
|
| Not_found -> Misc.fatal_error "Linearize.find_exit_label"
|
1995-06-15 01:17:29 -07:00
|
|
|
|
2000-10-02 07:08:30 -07:00
|
|
|
let is_next_catch n = match !exit_label with
|
|
|
|
| (n0,_)::_ when n0=n -> true
|
|
|
|
| _ -> false
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Linearize an instruction [i]: add it in front of the continuation [n] *)
|
|
|
|
|
|
|
|
let rec linear i n =
|
|
|
|
match i.Mach.desc with
|
|
|
|
Iend -> n
|
|
|
|
| Iop(Itailcall_ind | Itailcall_imm _ as op) ->
|
|
|
|
copy_instr (Lop op) i (discard_dead_code n)
|
1995-07-24 05:44:17 -07:00
|
|
|
| Iop(Imove | Ireload | Ispill)
|
|
|
|
when i.Mach.arg.(0).loc = i.Mach.res.(0).loc ->
|
|
|
|
linear i.Mach.next n
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iop op ->
|
|
|
|
copy_instr (Lop op) i (linear i.Mach.next n)
|
|
|
|
| Ireturn ->
|
1995-08-25 01:46:03 -07:00
|
|
|
let n1 = copy_instr Lreturn i (discard_dead_code n) in
|
|
|
|
if !Proc.contains_calls
|
|
|
|
then cons_instr Lreloadretaddr n1
|
|
|
|
else n1
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iifthenelse(test, ifso, ifnot) ->
|
|
|
|
let n1 = linear i.Mach.next n in
|
1998-04-06 02:08:26 -07:00
|
|
|
begin match (ifso.Mach.desc, ifnot.Mach.desc, n1.desc) with
|
|
|
|
Iend, _, Lbranch lbl ->
|
|
|
|
copy_instr (Lcondbranch(test, lbl)) i (linear ifnot n1)
|
|
|
|
| _, Iend, Lbranch lbl ->
|
|
|
|
copy_instr (Lcondbranch(invert_test test, lbl)) i (linear ifso n1)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Iexit nfail1, Iexit nfail2, _
|
|
|
|
when is_next_catch nfail1 ->
|
|
|
|
let lbl2 = find_exit_label nfail2 in
|
|
|
|
copy_instr
|
|
|
|
(Lcondbranch (invert_test test, lbl2)) i (linear ifso n1)
|
2000-08-11 12:50:59 -07:00
|
|
|
| Iexit nfail, _, _ ->
|
2000-10-02 07:08:30 -07:00
|
|
|
let n2 = linear ifnot n1
|
|
|
|
and lbl = find_exit_label nfail in
|
2000-08-11 12:50:59 -07:00
|
|
|
copy_instr (Lcondbranch(test, lbl)) i n2
|
|
|
|
| _, Iexit nfail, _ ->
|
2000-01-07 07:50:42 -08:00
|
|
|
let n2 = linear ifso n1 in
|
2000-08-11 12:50:59 -07:00
|
|
|
let lbl = find_exit_label nfail in
|
|
|
|
copy_instr (Lcondbranch(invert_test test, lbl)) i n2
|
1998-04-06 02:08:26 -07:00
|
|
|
| Iend, _, _ ->
|
1995-06-15 01:17:29 -07:00
|
|
|
let (lbl_end, n2) = get_label n1 in
|
1998-04-06 02:08:26 -07:00
|
|
|
copy_instr (Lcondbranch(test, lbl_end)) i (linear ifnot n2)
|
|
|
|
| _, Iend, _ ->
|
1995-06-15 01:17:29 -07:00
|
|
|
let (lbl_end, n2) = get_label n1 in
|
|
|
|
copy_instr (Lcondbranch(invert_test test, lbl_end)) i
|
1998-04-06 02:08:26 -07:00
|
|
|
(linear ifso n2)
|
|
|
|
| _, _, _ ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Should attempt branch prediction here *)
|
|
|
|
let (lbl_end, n2) = get_label n1 in
|
|
|
|
let (lbl_else, nelse) = get_label (linear ifnot n2) in
|
|
|
|
copy_instr (Lcondbranch(invert_test test, lbl_else)) i
|
|
|
|
(linear ifso (add_branch lbl_end nelse))
|
|
|
|
end
|
|
|
|
| Iswitch(index, cases) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let lbl_cases = Array.create (Array.length cases) 0 in
|
1995-06-15 01:17:29 -07:00
|
|
|
let (lbl_end, n1) = get_label(linear i.Mach.next n) in
|
1995-07-02 09:41:48 -07:00
|
|
|
let n2 = ref (discard_dead_code n1) in
|
1995-06-15 01:17:29 -07:00
|
|
|
for i = Array.length cases - 1 downto 0 do
|
|
|
|
let (lbl_case, ncase) =
|
|
|
|
get_label(linear cases.(i) (add_branch lbl_end !n2)) in
|
|
|
|
lbl_cases.(i) <- lbl_case;
|
1995-07-02 09:41:48 -07:00
|
|
|
n2 := discard_dead_code ncase
|
1995-06-15 01:17:29 -07:00
|
|
|
done;
|
1995-08-12 07:26:23 -07:00
|
|
|
(* Switches with 1 and 2 branches have been eliminated earlier.
|
|
|
|
Here, we do something for switches with 3 branches. *)
|
|
|
|
if Array.length index = 3 then begin
|
2000-10-02 07:08:30 -07:00
|
|
|
let fallthrough_lbl = check_label !n2 in
|
1995-08-12 07:26:23 -07:00
|
|
|
let find_label n =
|
|
|
|
let lbl = lbl_cases.(index.(n)) in
|
|
|
|
if lbl = fallthrough_lbl then None else Some lbl in
|
|
|
|
copy_instr (Lcondbranch3(find_label 0, find_label 1, find_label 2))
|
|
|
|
i !n2
|
|
|
|
end else
|
|
|
|
copy_instr (Lswitch(Array.map (fun n -> lbl_cases.(n)) index)) i !n2
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iloop body ->
|
|
|
|
let lbl_head = new_label() in
|
|
|
|
let n1 = linear i.Mach.next n in
|
1995-06-15 09:08:53 -07:00
|
|
|
let n2 = linear body (cons_instr (Lbranch lbl_head) n1) in
|
1995-06-15 01:17:29 -07:00
|
|
|
cons_instr (Llabel lbl_head) n2
|
2000-08-11 12:50:59 -07:00
|
|
|
| Icatch(io, body, handler) ->
|
1995-06-15 01:17:29 -07:00
|
|
|
let (lbl_end, n1) = get_label(linear i.Mach.next n) in
|
|
|
|
let (lbl_handler, n2) = get_label(linear handler n1) in
|
2000-08-11 12:50:59 -07:00
|
|
|
exit_label := (io, lbl_handler) :: !exit_label ;
|
1995-06-15 01:17:29 -07:00
|
|
|
let n3 = linear body (add_branch lbl_end n2) in
|
2000-08-11 12:50:59 -07:00
|
|
|
exit_label := List.tl !exit_label;
|
1995-06-15 01:17:29 -07:00
|
|
|
n3
|
2000-08-11 12:50:59 -07:00
|
|
|
| Iexit nfail ->
|
1999-11-30 08:07:38 -08:00
|
|
|
let n1 = linear i.Mach.next n in
|
2000-08-11 12:50:59 -07:00
|
|
|
let lbl = find_exit_label nfail in
|
|
|
|
add_branch lbl n1
|
1995-06-15 01:17:29 -07:00
|
|
|
| Itrywith(body, handler) ->
|
1995-07-07 05:07:07 -07:00
|
|
|
let (lbl_join, n1) = get_label (linear i.Mach.next n) in
|
|
|
|
let (lbl_body, n2) =
|
|
|
|
get_label (cons_instr Lpushtrap
|
|
|
|
(linear body (cons_instr Lpoptrap n1))) in
|
|
|
|
cons_instr (Lsetuptrap lbl_body)
|
|
|
|
(linear handler (add_branch lbl_join n2))
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iraise ->
|
1995-07-24 05:44:17 -07:00
|
|
|
copy_instr Lraise i (discard_dead_code n)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let fundecl f =
|
|
|
|
{ fun_name = f.Mach.fun_name;
|
1995-07-02 09:41:48 -07:00
|
|
|
fun_body = linear f.Mach.fun_body end_instr;
|
|
|
|
fun_fast = f.Mach.fun_fast }
|