1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
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
|
|
|
(* Selection of pseudo-instructions, assignment of pseudo-registers,
|
|
|
|
sequentialization. *)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Cmm
|
1995-07-02 09:41:48 -07:00
|
|
|
open Reg
|
1995-06-15 01:17:29 -07:00
|
|
|
open Mach
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
type environment = (Ident.t, Reg.t array) Tbl.t
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
(* Infer the type of the result of an operation *)
|
|
|
|
|
|
|
|
let oper_result_type = function
|
2007-01-29 04:11:18 -08:00
|
|
|
Capply(ty, _) -> ty
|
|
|
|
| Cextcall(s, ty, alloc, _) -> ty
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cload c ->
|
|
|
|
begin match c with
|
|
|
|
Word -> typ_addr
|
2000-03-10 06:31:06 -08:00
|
|
|
| Single | Double | Double_u -> typ_float
|
2000-02-04 04:43:18 -08:00
|
|
|
| _ -> typ_int
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Calloc -> typ_addr
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cstore c -> typ_void
|
1998-04-06 02:08:26 -07:00
|
|
|
| Caddi | Csubi | Cmuli | Cdivi | Cmodi |
|
|
|
|
Cand | Cor | Cxor | Clsl | Clsr | Casr |
|
|
|
|
Ccmpi _ | Ccmpa _ | Ccmpf _ -> typ_int
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cadda | Csuba -> typ_addr
|
1996-03-07 05:45:17 -08:00
|
|
|
| Cnegf | Cabsf | Caddf | Csubf | Cmulf | Cdivf -> typ_float
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cfloatofint -> typ_float
|
|
|
|
| Cintoffloat -> typ_int
|
2007-01-29 04:11:18 -08:00
|
|
|
| Craise _ -> typ_void
|
|
|
|
| Ccheckbound _ -> typ_void
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Infer the size in bytes of the result of a simple expression *)
|
|
|
|
|
1999-05-15 08:04:46 -07:00
|
|
|
let size_expr env exp =
|
|
|
|
let rec size localenv = function
|
|
|
|
Cconst_int _ | Cconst_natint _ -> Arch.size_int
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cconst_symbol _ | Cconst_pointer _ | Cconst_natpointer _ ->
|
|
|
|
Arch.size_addr
|
1999-05-15 08:04:46 -07:00
|
|
|
| Cconst_float _ -> Arch.size_float
|
|
|
|
| Cvar id ->
|
|
|
|
begin try
|
|
|
|
Tbl.find id localenv
|
|
|
|
with Not_found ->
|
|
|
|
try
|
|
|
|
let regs = Tbl.find id env in
|
|
|
|
size_machtype (Array.map (fun r -> r.typ) regs)
|
|
|
|
with Not_found ->
|
|
|
|
fatal_error("Selection.size_expr: unbound var " ^
|
|
|
|
Ident.unique_name id)
|
|
|
|
end
|
|
|
|
| Ctuple el ->
|
|
|
|
List.fold_right (fun e sz -> size localenv e + sz) el 0
|
|
|
|
| Cop(op, args) ->
|
|
|
|
size_machtype(oper_result_type op)
|
|
|
|
| Clet(id, arg, body) ->
|
|
|
|
size (Tbl.add id (size localenv arg) localenv) body
|
|
|
|
| Csequence(e1, e2) ->
|
|
|
|
size localenv e2
|
|
|
|
| _ ->
|
|
|
|
fatal_error "Selection.size_expr"
|
|
|
|
in size Tbl.empty exp
|
1997-02-17 08:52:44 -08:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Swap the two arguments of an integer comparison *)
|
|
|
|
|
|
|
|
let swap_intcomp = function
|
|
|
|
Isigned cmp -> Isigned(swap_comparison cmp)
|
|
|
|
| Iunsigned cmp -> Iunsigned(swap_comparison cmp)
|
|
|
|
|
|
|
|
(* Naming of registers *)
|
|
|
|
|
|
|
|
let all_regs_anonymous rv =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length rv - 1 do
|
|
|
|
if String.length rv.(i).name > 0 then raise Exit
|
|
|
|
done;
|
|
|
|
true
|
|
|
|
with Exit ->
|
|
|
|
false
|
|
|
|
|
|
|
|
let name_regs id rv =
|
|
|
|
if Array.length rv = 1 then
|
|
|
|
rv.(0).name <- Ident.name id
|
|
|
|
else
|
|
|
|
for i = 0 to Array.length rv - 1 do
|
|
|
|
rv.(i).name <- Ident.name id ^ "#" ^ string_of_int i
|
|
|
|
done
|
|
|
|
|
|
|
|
(* "Join" two instruction sequences, making sure they return their results
|
|
|
|
in the same registers. *)
|
|
|
|
|
2002-11-04 08:25:09 -08:00
|
|
|
let join opt_r1 seq1 opt_r2 seq2 =
|
|
|
|
match (opt_r1, opt_r2) with
|
|
|
|
(None, _) -> opt_r2
|
|
|
|
| (_, None) -> opt_r1
|
|
|
|
| (Some r1, Some r2) ->
|
|
|
|
let l1 = Array.length r1 in
|
|
|
|
assert (l1 = Array.length r2);
|
|
|
|
let r = Array.create l1 Reg.dummy in
|
|
|
|
for i = 0 to l1-1 do
|
|
|
|
if String.length r1.(i).name = 0 then begin
|
|
|
|
r.(i) <- r1.(i);
|
|
|
|
seq2#insert_move r2.(i) r1.(i)
|
|
|
|
end else if String.length r2.(i).name = 0 then begin
|
|
|
|
r.(i) <- r2.(i);
|
|
|
|
seq1#insert_move r1.(i) r2.(i)
|
|
|
|
end else begin
|
|
|
|
r.(i) <- Reg.create r1.(i).typ;
|
|
|
|
seq1#insert_move r1.(i) r.(i);
|
|
|
|
seq2#insert_move r2.(i) r.(i)
|
|
|
|
end
|
|
|
|
done;
|
|
|
|
Some r
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
(* Same, for N branches *)
|
|
|
|
|
|
|
|
let join_array rs =
|
2002-11-04 08:25:09 -08:00
|
|
|
let some_res = ref None in
|
1997-07-24 04:49:12 -07:00
|
|
|
for i = 0 to Array.length rs - 1 do
|
|
|
|
let (r, s) = rs.(i) in
|
2002-11-04 08:25:09 -08:00
|
|
|
if r <> None then some_res := r
|
1997-07-24 04:49:12 -07:00
|
|
|
done;
|
2002-11-04 08:25:09 -08:00
|
|
|
match !some_res with
|
|
|
|
None -> None
|
|
|
|
| Some template ->
|
|
|
|
let size_res = Array.length template in
|
|
|
|
let res = Array.create size_res Reg.dummy in
|
|
|
|
for i = 0 to size_res - 1 do
|
|
|
|
res.(i) <- Reg.create template.(i).typ
|
|
|
|
done;
|
|
|
|
for i = 0 to Array.length rs - 1 do
|
|
|
|
let (r, s) = rs.(i) in
|
|
|
|
match r with
|
|
|
|
None -> ()
|
|
|
|
| Some r -> s#insert_moves r res
|
|
|
|
done;
|
|
|
|
Some res
|
1997-07-24 04:49:12 -07:00
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
(* Extract debug info contained in a C-- operation *)
|
|
|
|
let debuginfo_op = function
|
|
|
|
| Capply(_, dbg) -> dbg
|
|
|
|
| Cextcall(_, _, _, dbg) -> dbg
|
|
|
|
| Craise dbg -> dbg
|
|
|
|
| Ccheckbound dbg -> dbg
|
|
|
|
| _ -> Debuginfo.none
|
|
|
|
|
2001-02-05 00:48:44 -08:00
|
|
|
(* Registers for catch constructs *)
|
2000-10-02 07:08:30 -07:00
|
|
|
let catch_regs = ref []
|
|
|
|
|
2000-10-10 05:39:16 -07:00
|
|
|
(* Name of function being compiled *)
|
|
|
|
let current_function_name = ref ""
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* The default instruction selection class *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
class virtual selector_generic = object (self)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
2006-04-16 16:28:22 -07:00
|
|
|
(* Says if an expression is "simple". A "simple" expression has no
|
|
|
|
side-effects and its execution can be delayed until its value
|
|
|
|
is really needed. In the case of e.g. an [alloc] instruction,
|
|
|
|
the non-simple arguments are computed in right-to-left order
|
|
|
|
first, then the block is allocated, then the simple arguments are
|
|
|
|
evaluated and stored. *)
|
|
|
|
|
|
|
|
method is_simple_expr = function
|
|
|
|
Cconst_int _ -> true
|
|
|
|
| Cconst_natint _ -> true
|
|
|
|
| Cconst_float _ -> true
|
|
|
|
| Cconst_symbol _ -> true
|
|
|
|
| Cconst_pointer _ -> true
|
|
|
|
| Cconst_natpointer _ -> true
|
|
|
|
| Cvar _ -> true
|
|
|
|
| Ctuple el -> List.for_all self#is_simple_expr el
|
|
|
|
| Clet(id, arg, body) -> self#is_simple_expr arg && self#is_simple_expr body
|
|
|
|
| Csequence(e1, e2) -> self#is_simple_expr e1 && self#is_simple_expr e2
|
|
|
|
| Cop(op, args) ->
|
|
|
|
begin match op with
|
|
|
|
(* The following may have side effects *)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Capply _ | Cextcall _ | Calloc | Cstore _ | Craise _ -> false
|
2006-04-16 16:28:22 -07:00
|
|
|
(* The remaining operations are simple if their args are *)
|
|
|
|
| _ ->
|
|
|
|
List.for_all self#is_simple_expr args
|
|
|
|
end
|
|
|
|
| _ -> false
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Says whether an integer constant is a suitable immediate argument *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method virtual is_immediate : int -> bool
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
(* Selection of addressing modes *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method virtual select_addressing :
|
1997-07-24 04:49:12 -07:00
|
|
|
Cmm.expression -> Arch.addressing_mode * Cmm.expression
|
|
|
|
|
2000-02-04 04:43:18 -08:00
|
|
|
(* Default instruction selection for stores (of words) *)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
method select_store addr arg =
|
|
|
|
(Istore(Word, addr), arg)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Default instruction selection for operators *)
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method select_operation op args =
|
1995-06-15 01:17:29 -07:00
|
|
|
match (op, args) with
|
2007-01-29 04:11:18 -08:00
|
|
|
(Capply(ty, dbg), Cconst_symbol s :: rem) -> (Icall_imm s, rem)
|
|
|
|
| (Capply(ty, dbg), _) -> (Icall_ind, args)
|
|
|
|
| (Cextcall(s, ty, alloc, dbg), _) -> (Iextcall(s, alloc), args)
|
2000-02-04 04:43:18 -08:00
|
|
|
| (Cload chunk, [arg]) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (addr, eloc) = self#select_addressing arg in
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iload(chunk, addr), [eloc])
|
2000-02-04 04:43:18 -08:00
|
|
|
| (Cstore chunk, [arg1; arg2]) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (addr, eloc) = self#select_addressing arg1 in
|
2000-02-04 04:43:18 -08:00
|
|
|
if chunk = Word then begin
|
|
|
|
let (op, newarg2) = self#select_store addr arg2 in
|
|
|
|
(op, [newarg2; eloc])
|
|
|
|
end else begin
|
|
|
|
(Istore(chunk, addr), [arg2; eloc])
|
|
|
|
(* Inversion addr/datum in Istore *)
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| (Calloc, _) -> (Ialloc 0, args)
|
1997-07-24 04:49:12 -07:00
|
|
|
| (Caddi, _) -> self#select_arith_comm Iadd args
|
|
|
|
| (Csubi, _) -> self#select_arith Isub args
|
1995-07-25 06:04:41 -07:00
|
|
|
| (Cmuli, [arg1; Cconst_int n]) ->
|
|
|
|
let l = Misc.log2 n in
|
|
|
|
if n = 1 lsl l
|
1995-11-19 08:53:12 -08:00
|
|
|
then (Iintop_imm(Ilsl, l), [arg1])
|
1997-07-24 04:49:12 -07:00
|
|
|
else self#select_arith_comm Imul args
|
1995-11-19 08:53:12 -08:00
|
|
|
| (Cmuli, [Cconst_int n; arg1]) ->
|
|
|
|
let l = Misc.log2 n in
|
|
|
|
if n = 1 lsl l
|
1995-07-25 06:04:41 -07:00
|
|
|
then (Iintop_imm(Ilsl, l), [arg1])
|
1997-07-24 04:49:12 -07:00
|
|
|
else self#select_arith_comm Imul args
|
|
|
|
| (Cmuli, _) -> self#select_arith_comm Imul args
|
|
|
|
| (Cdivi, _) -> self#select_arith Idiv args
|
|
|
|
| (Cmodi, _) -> self#select_arith_comm Imod args
|
|
|
|
| (Cand, _) -> self#select_arith_comm Iand args
|
|
|
|
| (Cor, _) -> self#select_arith_comm Ior args
|
|
|
|
| (Cxor, _) -> self#select_arith_comm Ixor args
|
|
|
|
| (Clsl, _) -> self#select_shift Ilsl args
|
|
|
|
| (Clsr, _) -> self#select_shift Ilsr args
|
|
|
|
| (Casr, _) -> self#select_shift Iasr args
|
|
|
|
| (Ccmpi comp, _) -> self#select_arith_comp (Isigned comp) args
|
|
|
|
| (Cadda, _) -> self#select_arith_comm Iadd args
|
|
|
|
| (Csuba, _) -> self#select_arith Isub args
|
|
|
|
| (Ccmpa comp, _) -> self#select_arith_comp (Iunsigned comp) args
|
1996-03-07 05:45:17 -08:00
|
|
|
| (Cnegf, _) -> (Inegf, args)
|
|
|
|
| (Cabsf, _) -> (Iabsf, args)
|
1995-07-02 09:41:48 -07:00
|
|
|
| (Caddf, _) -> (Iaddf, args)
|
2010-01-22 04:48:24 -08:00
|
|
|
| (Csubf, _) -> (Isubf, args)
|
|
|
|
| (Cmulf, _) -> (Imulf, args)
|
1995-07-02 09:41:48 -07:00
|
|
|
| (Cdivf, _) -> (Idivf, args)
|
|
|
|
| (Cfloatofint, _) -> (Ifloatofint, args)
|
|
|
|
| (Cintoffloat, _) -> (Iintoffloat, args)
|
2007-01-29 04:11:18 -08:00
|
|
|
| (Ccheckbound _, _) -> self#select_arith Icheckbound args
|
1997-07-24 04:49:12 -07:00
|
|
|
| _ -> fatal_error "Selection.select_oper"
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private select_arith_comm op = function
|
1997-07-24 04:49:12 -07:00
|
|
|
[arg; Cconst_int n] when self#is_immediate n ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop_imm(op, n), [arg])
|
2000-03-17 05:24:17 -08:00
|
|
|
| [arg; Cconst_pointer n] when self#is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
1997-07-24 04:49:12 -07:00
|
|
|
| [Cconst_int n; arg] when self#is_immediate n ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop_imm(op, n), [arg])
|
2000-03-17 05:24:17 -08:00
|
|
|
| [Cconst_pointer n; arg] when self#is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
1995-06-15 01:17:29 -07:00
|
|
|
| args ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop op, args)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private select_arith op = function
|
1997-07-24 04:49:12 -07:00
|
|
|
[arg; Cconst_int n] when self#is_immediate n ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop_imm(op, n), [arg])
|
2000-03-17 05:24:17 -08:00
|
|
|
| [arg; Cconst_pointer n] when self#is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
1995-06-15 01:17:29 -07:00
|
|
|
| args ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop op, args)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private select_shift op = function
|
2000-12-28 05:07:42 -08:00
|
|
|
[arg; Cconst_int n] when n >= 0 && n < Arch.size_int * 8 ->
|
1995-09-11 08:15:03 -07:00
|
|
|
(Iintop_imm(op, n), [arg])
|
|
|
|
| args ->
|
|
|
|
(Iintop op, args)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private select_arith_comp cmp = function
|
1997-07-24 04:49:12 -07:00
|
|
|
[arg; Cconst_int n] when self#is_immediate n ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop_imm(Icomp cmp, n), [arg])
|
2000-03-17 05:24:17 -08:00
|
|
|
| [arg; Cconst_pointer n] when self#is_immediate n ->
|
|
|
|
(Iintop_imm(Icomp cmp, n), [arg])
|
1997-07-24 04:49:12 -07:00
|
|
|
| [Cconst_int n; arg] when self#is_immediate n ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop_imm(Icomp(swap_intcomp cmp), n), [arg])
|
2000-03-17 05:24:17 -08:00
|
|
|
| [Cconst_pointer n; arg] when self#is_immediate n ->
|
|
|
|
(Iintop_imm(Icomp(swap_intcomp cmp), n), [arg])
|
1995-06-15 01:17:29 -07:00
|
|
|
| args ->
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iintop(Icomp cmp), args)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
(* Instruction selection for conditionals *)
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method select_condition = function
|
|
|
|
Cop(Ccmpi cmp, [arg1; Cconst_int n]) when self#is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Isigned cmp, n), arg1)
|
1997-07-24 04:49:12 -07:00
|
|
|
| Cop(Ccmpi cmp, [Cconst_int n; arg2]) when self#is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Isigned(swap_comparison cmp), n), arg2)
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cop(Ccmpi cmp, [arg1; Cconst_pointer n]) when self#is_immediate n ->
|
|
|
|
(Iinttest_imm(Isigned cmp, n), arg1)
|
|
|
|
| Cop(Ccmpi cmp, [Cconst_pointer n; arg2]) when self#is_immediate n ->
|
|
|
|
(Iinttest_imm(Isigned(swap_comparison cmp), n), arg2)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cop(Ccmpi cmp, args) ->
|
|
|
|
(Iinttest(Isigned cmp), Ctuple args)
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cop(Ccmpa cmp, [arg1; Cconst_pointer n]) when self#is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Iunsigned cmp, n), arg1)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Cop(Ccmpa cmp, [arg1; Cconst_int n]) when self#is_immediate n ->
|
|
|
|
(Iinttest_imm(Iunsigned cmp, n), arg1)
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cop(Ccmpa cmp, [Cconst_pointer n; arg2]) when self#is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Iunsigned(swap_comparison cmp), n), arg2)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Cop(Ccmpa cmp, [Cconst_int n; arg2]) when self#is_immediate n ->
|
|
|
|
(Iinttest_imm(Iunsigned(swap_comparison cmp), n), arg2)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cop(Ccmpa cmp, args) ->
|
|
|
|
(Iinttest(Iunsigned cmp), Ctuple args)
|
|
|
|
| Cop(Ccmpf cmp, args) ->
|
1995-11-28 06:25:57 -08:00
|
|
|
(Ifloattest(cmp, false), Ctuple args)
|
1995-07-17 09:10:15 -07:00
|
|
|
| Cop(Cand, [arg; Cconst_int 1]) ->
|
|
|
|
(Ioddtest, arg)
|
1995-06-15 01:17:29 -07:00
|
|
|
| arg ->
|
|
|
|
(Itruetest, arg)
|
|
|
|
|
2009-03-31 02:44:50 -07:00
|
|
|
(* Return an array of fresh registers of the given type.
|
|
|
|
Normally implemented as Reg.createv, but some
|
|
|
|
ports (e.g. Arm) can override this definition to store float values
|
|
|
|
in pairs of integer registers. *)
|
|
|
|
|
|
|
|
method regs_for tys = Reg.createv tys
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Buffering of instruction sequences *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
val mutable instr_seq = dummy_instr
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
method insert_debug desc dbg arg res =
|
|
|
|
instr_seq <- instr_cons_debug desc arg res dbg instr_seq
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method insert desc arg res =
|
|
|
|
instr_seq <- instr_cons desc arg res instr_seq
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method extract =
|
1995-07-02 09:41:48 -07:00
|
|
|
let rec extract res i =
|
|
|
|
if i == dummy_instr
|
|
|
|
then res
|
2007-01-29 04:11:18 -08:00
|
|
|
else extract {i with next = res} i.next in
|
1997-07-24 04:49:12 -07:00
|
|
|
extract (end_instr()) instr_seq
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Insert a sequence of moves from one pseudoreg set to another. *)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method insert_move src dst =
|
1995-07-02 09:41:48 -07:00
|
|
|
if src.stamp <> dst.stamp then
|
1997-07-24 04:49:12 -07:00
|
|
|
self#insert (Iop Imove) [|src|] [|dst|]
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method insert_moves src dst =
|
1995-07-02 09:41:48 -07:00
|
|
|
for i = 0 to Array.length src - 1 do
|
1997-07-24 04:49:12 -07:00
|
|
|
self#insert_move src.(i) dst.(i)
|
1995-07-02 09:41:48 -07:00
|
|
|
done
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Insert moves and stack offsets for function arguments and results *)
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method insert_move_args arg loc stacksize =
|
|
|
|
if stacksize <> 0 then self#insert (Iop(Istackoffset stacksize)) [||] [||];
|
|
|
|
self#insert_moves arg loc
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method insert_move_results loc res stacksize =
|
|
|
|
if stacksize <> 0 then self#insert(Iop(Istackoffset(-stacksize))) [||] [||];
|
|
|
|
self#insert_moves loc res
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2010-05-21 05:00:49 -07:00
|
|
|
(* Add an Iop opcode. Can be overridden by processor description
|
2010-01-22 04:48:24 -08:00
|
|
|
to insert moves before and after the operation, i.e. for two-address
|
1995-12-10 01:31:57 -08:00
|
|
|
instructions, or instructions using dedicated registers. *)
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
method insert_op_debug op dbg rs rd =
|
|
|
|
self#insert_debug (Iop op) dbg rs rd;
|
|
|
|
rd
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method insert_op op rs rd =
|
|
|
|
self#insert (Iop op) rs rd;
|
|
|
|
rd
|
1995-12-10 01:31:57 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Add the instructions for the given expression
|
1997-07-24 04:49:12 -07:00
|
|
|
at the end of the self sequence *)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method emit_expr env exp =
|
1995-07-02 09:41:48 -07:00
|
|
|
match exp with
|
|
|
|
Cconst_int n ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_int in
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(self#insert_op (Iconst_int(Nativeint.of_int n)) [||] r)
|
1997-03-04 02:19:51 -08:00
|
|
|
| Cconst_natint n ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_int in
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(self#insert_op (Iconst_int n) [||] r)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cconst_float n ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_float in
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(self#insert_op (Iconst_float n) [||] r)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cconst_symbol n ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_addr in
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(self#insert_op (Iconst_symbol n) [||] r)
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cconst_pointer n ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_addr in
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(self#insert_op (Iconst_int(Nativeint.of_int n)) [||] r)
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cconst_natpointer n ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_addr in
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(self#insert_op (Iconst_int n) [||] r)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cvar v ->
|
1995-07-02 09:41:48 -07:00
|
|
|
begin try
|
2002-11-04 08:25:09 -08:00
|
|
|
Some(Tbl.find v env)
|
1995-07-02 09:41:48 -07:00
|
|
|
with Not_found ->
|
1998-06-09 06:40:34 -07:00
|
|
|
fatal_error("Selection.emit_expr: unbound var " ^ Ident.unique_name v)
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clet(v, e1, e2) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env e1 with
|
|
|
|
None -> None
|
|
|
|
| Some r1 -> self#emit_expr (self#bind_let env v r1) e2
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cassign(v, e1) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let rv =
|
|
|
|
try
|
|
|
|
Tbl.find v env
|
|
|
|
with Not_found ->
|
|
|
|
fatal_error ("Selection.emit_expr: unbound var " ^ Ident.name v) in
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env e1 with
|
|
|
|
None -> None
|
|
|
|
| Some r1 -> self#insert_moves r1 rv; Some [||]
|
|
|
|
end
|
1996-07-25 06:15:16 -07:00
|
|
|
| Ctuple [] ->
|
2002-11-04 08:25:09 -08:00
|
|
|
Some [||]
|
1995-07-02 09:41:48 -07:00
|
|
|
| Ctuple exp_list ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_parts_list env exp_list with
|
|
|
|
None -> None
|
|
|
|
| Some(simple_list, ext_env) ->
|
|
|
|
Some(self#emit_tuple ext_env simple_list)
|
|
|
|
end
|
2007-01-29 04:11:18 -08:00
|
|
|
| Cop(Craise dbg, [arg]) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env arg with
|
|
|
|
None -> None
|
|
|
|
| Some r1 ->
|
|
|
|
let rd = [|Proc.loc_exn_bucket|] in
|
|
|
|
self#insert (Iop Imove) r1 rd;
|
2007-01-29 04:11:18 -08:00
|
|
|
self#insert_debug Iraise dbg rd [||];
|
2002-11-04 08:25:09 -08:00
|
|
|
None
|
|
|
|
end
|
1995-07-11 11:03:29 -07:00
|
|
|
| Cop(Ccmpf comp, args) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
self#emit_expr env (Cifthenelse(exp, Cconst_int 1, Cconst_int 0))
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cop(op, args) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_parts_list env args with
|
|
|
|
None -> None
|
|
|
|
| Some(simple_args, env) ->
|
|
|
|
let ty = oper_result_type op in
|
|
|
|
let (new_op, new_args) = self#select_operation op simple_args in
|
2007-01-29 04:11:18 -08:00
|
|
|
let dbg = debuginfo_op op in
|
2002-11-04 08:25:09 -08:00
|
|
|
match new_op with
|
|
|
|
Icall_ind ->
|
|
|
|
Proc.contains_calls := true;
|
|
|
|
let r1 = self#emit_tuple env new_args in
|
|
|
|
let rarg = Array.sub r1 1 (Array.length r1 - 1) in
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for ty in
|
2002-11-04 08:25:09 -08:00
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments rarg in
|
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
self#insert_move_args rarg loc_arg stack_ofs;
|
2007-01-29 04:11:18 -08:00
|
|
|
self#insert_debug (Iop Icall_ind) dbg
|
2002-11-04 08:25:09 -08:00
|
|
|
(Array.append [|r1.(0)|] loc_arg) loc_res;
|
|
|
|
self#insert_move_results loc_res rd stack_ofs;
|
|
|
|
Some rd
|
|
|
|
| Icall_imm lbl ->
|
|
|
|
Proc.contains_calls := true;
|
|
|
|
let r1 = self#emit_tuple env new_args in
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for ty in
|
2002-11-04 08:25:09 -08:00
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments r1 in
|
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
self#insert_move_args r1 loc_arg stack_ofs;
|
2007-01-29 04:11:18 -08:00
|
|
|
self#insert_debug (Iop(Icall_imm lbl)) dbg loc_arg loc_res;
|
2002-11-04 08:25:09 -08:00
|
|
|
self#insert_move_results loc_res rd stack_ofs;
|
|
|
|
Some rd
|
|
|
|
| Iextcall(lbl, alloc) ->
|
|
|
|
Proc.contains_calls := true;
|
|
|
|
let (loc_arg, stack_ofs) =
|
|
|
|
self#emit_extcall_args env new_args in
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for ty in
|
2002-11-04 08:25:09 -08:00
|
|
|
let loc_res = Proc.loc_external_results rd in
|
2007-01-29 04:11:18 -08:00
|
|
|
self#insert_debug (Iop(Iextcall(lbl, alloc))) dbg
|
|
|
|
loc_arg loc_res;
|
2002-11-04 08:25:09 -08:00
|
|
|
self#insert_move_results loc_res rd stack_ofs;
|
|
|
|
Some rd
|
|
|
|
| Ialloc _ ->
|
|
|
|
Proc.contains_calls := true;
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for typ_addr in
|
2002-11-04 08:25:09 -08:00
|
|
|
let size = size_expr env (Ctuple new_args) in
|
|
|
|
self#insert (Iop(Ialloc size)) [||] rd;
|
|
|
|
self#emit_stores env new_args rd;
|
|
|
|
Some rd
|
|
|
|
| op ->
|
|
|
|
let r1 = self#emit_tuple env new_args in
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for ty in
|
2007-01-29 04:11:18 -08:00
|
|
|
Some (self#insert_op_debug op dbg r1 rd)
|
2010-01-22 04:48:24 -08:00
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Csequence(e1, e2) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env e1 with
|
|
|
|
None -> None
|
|
|
|
| Some r1 -> self#emit_expr env e2
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cifthenelse(econd, eif, eelse) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (cond, earg) = self#select_condition econd in
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env earg with
|
|
|
|
None -> None
|
|
|
|
| Some rarg ->
|
|
|
|
let (rif, sif) = self#emit_sequence env eif in
|
|
|
|
let (relse, selse) = self#emit_sequence env eelse in
|
|
|
|
let r = join rif sif relse selse in
|
|
|
|
self#insert (Iifthenelse(cond, sif#extract, selse#extract))
|
|
|
|
rarg [||];
|
|
|
|
r
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cswitch(esel, index, ecases) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env esel with
|
|
|
|
None -> None
|
|
|
|
| Some rsel ->
|
|
|
|
let rscases = Array.map (self#emit_sequence env) ecases in
|
|
|
|
let r = join_array rscases in
|
|
|
|
self#insert (Iswitch(index,
|
|
|
|
Array.map (fun (r, s) -> s#extract) rscases))
|
|
|
|
rsel [||];
|
|
|
|
r
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cloop(ebody) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (rarg, sbody) = self#emit_sequence env ebody in
|
|
|
|
self#insert (Iloop(sbody#extract)) [||] [||];
|
2002-11-04 08:25:09 -08:00
|
|
|
Some [||]
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ccatch(nfail, ids, e1, e2) ->
|
|
|
|
let rs =
|
|
|
|
List.map
|
|
|
|
(fun id ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_addr in name_regs id r; r)
|
2000-10-02 07:08:30 -07:00
|
|
|
ids in
|
2010-01-22 04:48:24 -08:00
|
|
|
catch_regs := (nfail, Array.concat rs) :: !catch_regs ;
|
1997-07-24 04:49:12 -07:00
|
|
|
let (r1, s1) = self#emit_sequence env e1 in
|
2000-10-02 07:08:30 -07:00
|
|
|
catch_regs := List.tl !catch_regs ;
|
|
|
|
let new_env =
|
|
|
|
List.fold_left
|
|
|
|
(fun env (id,r) -> Tbl.add id r env)
|
|
|
|
env (List.combine ids rs) in
|
|
|
|
let (r2, s2) = self#emit_sequence new_env e2 in
|
1995-07-02 09:41:48 -07:00
|
|
|
let r = join r1 s1 r2 s2 in
|
2000-08-11 12:50:59 -07:00
|
|
|
self#insert (Icatch(nfail, s1#extract, s2#extract)) [||] [||];
|
1995-07-02 09:41:48 -07:00
|
|
|
r
|
2000-10-02 07:08:30 -07:00
|
|
|
| Cexit (nfail,args) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_parts_list env args with
|
|
|
|
None -> None
|
|
|
|
| Some (simple_list, ext_env) ->
|
|
|
|
let src = self#emit_tuple ext_env simple_list in
|
|
|
|
let dest =
|
|
|
|
try List.assoc nfail !catch_regs
|
|
|
|
with Not_found ->
|
|
|
|
Misc.fatal_error
|
|
|
|
("Selectgen.emit_expr, on exit("^string_of_int nfail^")") in
|
|
|
|
self#insert_moves src dest ;
|
|
|
|
self#insert (Iexit nfail) [||] [||];
|
|
|
|
None
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ctrywith(e1, v, e2) ->
|
1995-07-12 07:25:41 -07:00
|
|
|
Proc.contains_calls := true;
|
1997-07-24 04:49:12 -07:00
|
|
|
let (r1, s1) = self#emit_sequence env e1 in
|
2009-03-31 02:44:50 -07:00
|
|
|
let rv = self#regs_for typ_addr in
|
1997-07-24 04:49:12 -07:00
|
|
|
let (r2, s2) = self#emit_sequence (Tbl.add v rv env) e2 in
|
1995-07-02 09:41:48 -07:00
|
|
|
let r = join r1 s1 r2 s2 in
|
1997-07-24 04:49:12 -07:00
|
|
|
self#insert
|
|
|
|
(Itrywith(s1#extract,
|
1995-07-02 09:41:48 -07:00
|
|
|
instr_cons (Iop Imove) [|Proc.loc_exn_bucket|] rv
|
1998-06-24 12:22:26 -07:00
|
|
|
(s2#extract)))
|
1997-07-24 04:49:12 -07:00
|
|
|
[||] [||];
|
1995-07-02 09:41:48 -07:00
|
|
|
r
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private emit_sequence env exp =
|
1997-07-24 04:49:12 -07:00
|
|
|
let s = {< instr_seq = dummy_instr >} in
|
|
|
|
let r = s#emit_expr env exp in
|
|
|
|
(r, s)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2002-11-04 08:25:09 -08:00
|
|
|
method private bind_let env v r1 =
|
1995-07-02 09:41:48 -07:00
|
|
|
if all_regs_anonymous r1 then begin
|
|
|
|
name_regs v r1;
|
|
|
|
Tbl.add v r1 env
|
|
|
|
end else begin
|
2009-03-31 02:44:50 -07:00
|
|
|
let rv = Reg.createv_like r1 in
|
1995-07-02 09:41:48 -07:00
|
|
|
name_regs v rv;
|
1997-07-24 04:49:12 -07:00
|
|
|
self#insert_moves r1 rv;
|
1995-07-02 09:41:48 -07:00
|
|
|
Tbl.add v rv env
|
|
|
|
end
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private emit_parts env exp =
|
2006-04-16 16:28:22 -07:00
|
|
|
if self#is_simple_expr exp then
|
2002-11-04 08:25:09 -08:00
|
|
|
Some (exp, env)
|
1997-02-17 08:52:44 -08:00
|
|
|
else begin
|
2002-11-04 08:25:09 -08:00
|
|
|
match self#emit_expr env exp with
|
|
|
|
None -> None
|
|
|
|
| Some r ->
|
2009-03-31 02:44:50 -07:00
|
|
|
if Array.length r = 0 then
|
|
|
|
Some (Ctuple [], env)
|
|
|
|
else begin
|
|
|
|
(* The normal case *)
|
|
|
|
let id = Ident.create "bind" in
|
|
|
|
if all_regs_anonymous r then
|
|
|
|
(* r is an anonymous, unshared register; use it directly *)
|
|
|
|
Some (Cvar id, Tbl.add id r env)
|
|
|
|
else begin
|
|
|
|
(* Introduce a fresh temp to hold the result *)
|
|
|
|
let tmp = Reg.createv_like r in
|
|
|
|
self#insert_moves r tmp;
|
|
|
|
Some (Cvar id, Tbl.add id tmp env)
|
|
|
|
end
|
|
|
|
end
|
1997-02-17 08:52:44 -08:00
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private emit_parts_list env exp_list =
|
1995-07-02 09:41:48 -07:00
|
|
|
match exp_list with
|
2002-11-04 08:25:09 -08:00
|
|
|
[] -> Some ([], env)
|
1995-07-02 09:41:48 -07:00
|
|
|
| exp :: rem ->
|
|
|
|
(* This ensures right-to-left evaluation, consistent with the
|
|
|
|
bytecode compiler *)
|
2002-11-04 08:25:09 -08:00
|
|
|
match self#emit_parts_list env rem with
|
|
|
|
None -> None
|
|
|
|
| Some(new_rem, new_env) ->
|
|
|
|
match self#emit_parts new_env exp with
|
|
|
|
None -> None
|
|
|
|
| Some(new_exp, fin_env) -> Some(new_exp :: new_rem, fin_env)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private emit_tuple env exp_list =
|
1995-12-10 01:31:57 -08:00
|
|
|
let rec emit_list = function
|
|
|
|
[] -> []
|
|
|
|
| exp :: rem ->
|
|
|
|
(* Again, force right-to-left evaluation *)
|
|
|
|
let loc_rem = emit_list rem in
|
2002-11-04 08:25:09 -08:00
|
|
|
match self#emit_expr env exp with
|
|
|
|
None -> assert false (* should have been caught in emit_parts *)
|
|
|
|
| Some loc_exp -> loc_exp :: loc_rem in
|
1995-12-10 01:31:57 -08:00
|
|
|
Array.concat(emit_list exp_list)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method emit_extcall_args env args =
|
|
|
|
let r1 = self#emit_tuple env args in
|
|
|
|
let (loc_arg, stack_ofs as arg_stack) = Proc.loc_external_arguments r1 in
|
|
|
|
self#insert_move_args r1 loc_arg stack_ofs;
|
|
|
|
arg_stack
|
|
|
|
|
2000-06-29 04:44:36 -07:00
|
|
|
method emit_stores env data regs_addr =
|
|
|
|
let a =
|
|
|
|
ref (Arch.offset_addressing Arch.identity_addressing (-Arch.size_int)) in
|
1995-07-02 09:41:48 -07:00
|
|
|
List.iter
|
|
|
|
(fun e ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (op, arg) = self#select_store !a e in
|
2002-11-04 08:25:09 -08:00
|
|
|
match self#emit_expr env arg with
|
|
|
|
None -> assert false
|
|
|
|
| Some regs ->
|
|
|
|
match op with
|
|
|
|
Istore(_, _) ->
|
|
|
|
for i = 0 to Array.length regs - 1 do
|
|
|
|
let r = regs.(i) in
|
|
|
|
let kind = if r.typ = Float then Double_u else Word in
|
|
|
|
self#insert (Iop(Istore(kind, !a)))
|
|
|
|
(Array.append [|r|] regs_addr) [||];
|
|
|
|
a := Arch.offset_addressing !a (size_component r.typ)
|
|
|
|
done
|
|
|
|
| _ ->
|
|
|
|
self#insert (Iop op) (Array.append regs regs_addr) [||];
|
|
|
|
a := Arch.offset_addressing !a (size_expr env e))
|
1995-07-02 09:41:48 -07:00
|
|
|
data
|
|
|
|
|
|
|
|
(* Same, but in tail position *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private emit_return env exp =
|
2002-11-04 08:25:09 -08:00
|
|
|
match self#emit_expr env exp with
|
|
|
|
None -> ()
|
|
|
|
| Some r ->
|
|
|
|
let loc = Proc.loc_results r in
|
|
|
|
self#insert_moves r loc;
|
|
|
|
self#insert Ireturn loc [||]
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method emit_tail env exp =
|
1995-07-02 09:41:48 -07:00
|
|
|
match exp with
|
|
|
|
Clet(v, e1, e2) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env e1 with
|
|
|
|
None -> ()
|
|
|
|
| Some r1 -> self#emit_tail (self#bind_let env v r1) e2
|
|
|
|
end
|
2007-01-29 04:11:18 -08:00
|
|
|
| Cop(Capply(ty, dbg) as op, args) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_parts_list env args with
|
|
|
|
None -> ()
|
|
|
|
| Some(simple_args, env) ->
|
|
|
|
let (new_op, new_args) = self#select_operation op simple_args in
|
|
|
|
match new_op with
|
|
|
|
Icall_ind ->
|
|
|
|
let r1 = self#emit_tuple env new_args in
|
|
|
|
let rarg = Array.sub r1 1 (Array.length r1 - 1) in
|
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments rarg in
|
|
|
|
if stack_ofs = 0 then begin
|
|
|
|
self#insert_moves rarg loc_arg;
|
|
|
|
self#insert (Iop Itailcall_ind)
|
|
|
|
(Array.append [|r1.(0)|] loc_arg) [||]
|
|
|
|
end else begin
|
|
|
|
Proc.contains_calls := true;
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for ty in
|
2002-11-04 08:25:09 -08:00
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
self#insert_move_args rarg loc_arg stack_ofs;
|
2007-01-29 04:11:18 -08:00
|
|
|
self#insert_debug (Iop Icall_ind) dbg
|
2002-11-04 08:25:09 -08:00
|
|
|
(Array.append [|r1.(0)|] loc_arg) loc_res;
|
|
|
|
self#insert(Iop(Istackoffset(-stack_ofs))) [||] [||];
|
|
|
|
self#insert Ireturn loc_res [||]
|
|
|
|
end
|
|
|
|
| Icall_imm lbl ->
|
|
|
|
let r1 = self#emit_tuple env new_args in
|
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments r1 in
|
|
|
|
if stack_ofs = 0 then begin
|
|
|
|
self#insert_moves r1 loc_arg;
|
|
|
|
self#insert (Iop(Itailcall_imm lbl)) loc_arg [||]
|
|
|
|
end else if lbl = !current_function_name then begin
|
|
|
|
let loc_arg' = Proc.loc_parameters r1 in
|
|
|
|
self#insert_moves r1 loc_arg';
|
|
|
|
self#insert (Iop(Itailcall_imm lbl)) loc_arg' [||]
|
|
|
|
end else begin
|
|
|
|
Proc.contains_calls := true;
|
2009-03-31 02:44:50 -07:00
|
|
|
let rd = self#regs_for ty in
|
2002-11-04 08:25:09 -08:00
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
self#insert_move_args r1 loc_arg stack_ofs;
|
2007-01-29 04:11:18 -08:00
|
|
|
self#insert_debug (Iop(Icall_imm lbl)) dbg loc_arg loc_res;
|
2002-11-04 08:25:09 -08:00
|
|
|
self#insert(Iop(Istackoffset(-stack_ofs))) [||] [||];
|
|
|
|
self#insert Ireturn loc_res [||]
|
|
|
|
end
|
|
|
|
| _ -> fatal_error "Selection.emit_tail"
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
|
|
|
| Csequence(e1, e2) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env e1 with
|
|
|
|
None -> ()
|
|
|
|
| Some r1 -> self#emit_tail env e2
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cifthenelse(econd, eif, eelse) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (cond, earg) = self#select_condition econd in
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env earg with
|
|
|
|
None -> ()
|
|
|
|
| Some rarg ->
|
|
|
|
self#insert (Iifthenelse(cond, self#emit_tail_sequence env eif,
|
|
|
|
self#emit_tail_sequence env eelse))
|
|
|
|
rarg [||]
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cswitch(esel, index, ecases) ->
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match self#emit_expr env esel with
|
|
|
|
None -> ()
|
|
|
|
| Some rsel ->
|
|
|
|
self#insert
|
|
|
|
(Iswitch(index, Array.map (self#emit_tail_sequence env) ecases))
|
|
|
|
rsel [||]
|
|
|
|
end
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ccatch(nfail, ids, e1, e2) ->
|
|
|
|
let rs =
|
|
|
|
List.map
|
|
|
|
(fun id ->
|
2009-03-31 02:44:50 -07:00
|
|
|
let r = self#regs_for typ_addr in
|
2000-10-02 07:08:30 -07:00
|
|
|
name_regs id r ;
|
|
|
|
r)
|
|
|
|
ids in
|
2010-01-22 04:48:24 -08:00
|
|
|
catch_regs := (nfail, Array.concat rs) :: !catch_regs ;
|
2000-10-02 07:08:30 -07:00
|
|
|
let s1 = self#emit_tail_sequence env e1 in
|
|
|
|
catch_regs := List.tl !catch_regs ;
|
|
|
|
let new_env =
|
|
|
|
List.fold_left
|
|
|
|
(fun env (id,r) -> Tbl.add id r env)
|
|
|
|
env (List.combine ids rs) in
|
|
|
|
let s2 = self#emit_tail_sequence new_env e2 in
|
|
|
|
self#insert (Icatch(nfail, s1, s2)) [||] [||]
|
1995-07-24 05:44:17 -07:00
|
|
|
| Ctrywith(e1, v, e2) ->
|
|
|
|
Proc.contains_calls := true;
|
2002-11-04 08:25:09 -08:00
|
|
|
let (opt_r1, s1) = self#emit_sequence env e1 in
|
2009-03-31 02:44:50 -07:00
|
|
|
let rv = self#regs_for typ_addr in
|
1997-07-24 04:49:12 -07:00
|
|
|
let s2 = self#emit_tail_sequence (Tbl.add v rv env) e2 in
|
|
|
|
self#insert
|
|
|
|
(Itrywith(s1#extract,
|
1995-07-24 05:44:17 -07:00
|
|
|
instr_cons (Iop Imove) [|Proc.loc_exn_bucket|] rv s2))
|
1997-07-24 04:49:12 -07:00
|
|
|
[||] [||];
|
2002-11-04 08:25:09 -08:00
|
|
|
begin match opt_r1 with
|
|
|
|
None -> ()
|
|
|
|
| Some r1 ->
|
|
|
|
let loc = Proc.loc_results r1 in
|
|
|
|
self#insert_moves r1 loc;
|
|
|
|
self#insert Ireturn loc [||]
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| _ ->
|
1997-07-24 04:49:12 -07:00
|
|
|
self#emit_return env exp
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private emit_tail_sequence env exp =
|
1997-07-24 04:49:12 -07:00
|
|
|
let s = {< instr_seq = dummy_instr >} in
|
|
|
|
s#emit_tail env exp;
|
|
|
|
s#extract
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Sequentialization of a function definition *)
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method emit_fundecl f =
|
1995-07-02 09:41:48 -07:00
|
|
|
Proc.contains_calls := false;
|
2000-10-10 05:39:16 -07:00
|
|
|
current_function_name := f.Cmm.fun_name;
|
1995-07-02 09:41:48 -07:00
|
|
|
let rargs =
|
|
|
|
List.map
|
2009-03-31 02:44:50 -07:00
|
|
|
(fun (id, ty) -> let r = self#regs_for ty in name_regs id r; r)
|
1995-07-02 09:41:48 -07:00
|
|
|
f.Cmm.fun_args in
|
|
|
|
let rarg = Array.concat rargs in
|
|
|
|
let loc_arg = Proc.loc_parameters rarg in
|
|
|
|
let env =
|
|
|
|
List.fold_right2
|
|
|
|
(fun (id, ty) r env -> Tbl.add id r env)
|
|
|
|
f.Cmm.fun_args rargs Tbl.empty in
|
1997-07-24 04:49:12 -07:00
|
|
|
self#insert_moves loc_arg rarg;
|
|
|
|
self#emit_tail env f.Cmm.fun_body;
|
1995-07-02 09:41:48 -07:00
|
|
|
{ fun_name = f.Cmm.fun_name;
|
|
|
|
fun_args = loc_arg;
|
1997-07-24 04:49:12 -07:00
|
|
|
fun_body = self#extract;
|
1995-07-02 09:41:48 -07:00
|
|
|
fun_fast = f.Cmm.fun_fast }
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
end
|
2011-07-20 02:17:07 -07:00
|
|
|
|
|
|
|
(* Tail call criterion (estimated). Assumes:
|
|
|
|
- all arguments are of type "int" (always the case for Caml function calls)
|
|
|
|
- one extra argument representing the closure environment (conservative).
|
|
|
|
*)
|
|
|
|
|
|
|
|
let is_tail_call nargs =
|
|
|
|
assert (Reg.dummy.typ = Int);
|
|
|
|
let args = Array.make (nargs + 1) Reg.dummy in
|
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments args in
|
|
|
|
stack_ofs = 0
|
|
|
|
|
|
|
|
let _ =
|
|
|
|
Simplif.is_tail_native_heuristic := is_tail_call
|