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 *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $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
|
|
|
|
|
|
|
|
(* Infer the type of the result of an operation *)
|
|
|
|
|
|
|
|
let oper_result_type = function
|
|
|
|
Capply ty -> ty
|
1995-07-10 02:48:27 -07:00
|
|
|
| Cextcall(s, ty, alloc) -> ty
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cload ty -> ty
|
|
|
|
| Cloadchunk c -> typ_int
|
|
|
|
| Calloc -> typ_addr
|
|
|
|
| Cstore -> typ_void
|
|
|
|
| Cstorechunk c -> typ_void
|
|
|
|
| Caddi | Csubi | Cmuli | Cdivi | Cmodi
|
|
|
|
| Cand | Cor | Cxor | Clsl | Clsr | Casr
|
|
|
|
| Ccmpi _ | Ccmpa _ | Ccmpf _ -> typ_int
|
|
|
|
| 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
|
|
|
|
| Craise -> typ_void
|
1995-07-07 09:42:05 -07:00
|
|
|
| Ccheckbound -> typ_void
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> fatal_error "Selection.oper_result_type"
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Infer the size in bytes of the result of a simple expression *)
|
|
|
|
|
1997-02-17 08:52:44 -08:00
|
|
|
let size_expr env exp =
|
|
|
|
let rec size localenv = function
|
1997-03-04 02:19:51 -08:00
|
|
|
Cconst_int _ | Cconst_natint _ -> Arch.size_int
|
1997-02-17 08:52:44 -08:00
|
|
|
| Cconst_symbol _ | Cconst_pointer _ -> Arch.size_addr
|
|
|
|
| Cconst_float _ -> Arch.size_float
|
|
|
|
| Cvar id ->
|
|
|
|
begin try
|
|
|
|
Tbl.find id localenv
|
|
|
|
with Not_found ->
|
1995-07-02 09:41:48 -07:00
|
|
|
try
|
1997-02-17 08:52:44 -08:00
|
|
|
let regs = Tbl.find id env in
|
|
|
|
size_machtype (Array.map (fun r -> r.typ) regs)
|
1995-07-02 09:41:48 -07:00
|
|
|
with Not_found ->
|
1997-02-17 08:52:44 -08:00
|
|
|
fatal_error("Selection.size_expr: unbound var " ^ Ident.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
|
|
|
|
| _ ->
|
|
|
|
fatal_error "Selection.size_expr"
|
|
|
|
in size Tbl.empty exp
|
|
|
|
|
|
|
|
(* Says if an expression is "simple". A "simple" expression has no
|
|
|
|
side-effects and its execution can be delayed until its value
|
1995-07-11 01:51:10 -07:00
|
|
|
is really needed. In the case of e.g. an [alloc] instruction,
|
1997-02-17 08:52:44 -08:00
|
|
|
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. *)
|
|
|
|
|
|
|
|
let rec is_simple_expr = function
|
|
|
|
Cconst_int _ -> true
|
1997-03-04 02:19:51 -08:00
|
|
|
| Cconst_natint _ -> true
|
1997-02-17 08:52:44 -08:00
|
|
|
| Cconst_float _ -> true
|
|
|
|
| Cconst_symbol _ -> true
|
|
|
|
| Cconst_pointer _ -> true
|
|
|
|
| Cvar _ -> true
|
|
|
|
| Ctuple el -> List.for_all is_simple_expr el
|
|
|
|
| Clet(id, arg, body) -> is_simple_expr arg && is_simple_expr body
|
|
|
|
| Cop(op, args) ->
|
|
|
|
begin match op with
|
|
|
|
(* The following may have side effects *)
|
|
|
|
Capply _ | Cextcall(_, _, _) | Calloc | Cstore | Cstorechunk _ |
|
|
|
|
Craise -> false
|
1997-03-13 05:14:10 -08:00
|
|
|
(* The remaining operations are simple if their args are *)
|
|
|
|
| _ -> List.for_all is_simple_expr args
|
1997-02-17 08:52:44 -08:00
|
|
|
end
|
|
|
|
| _ -> false
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Default instruction selection for operators *)
|
|
|
|
|
|
|
|
let rec sel_operation op args =
|
1995-06-15 01:17:29 -07:00
|
|
|
match (op, args) with
|
1995-07-02 09:41:48 -07:00
|
|
|
(Capply ty, Cconst_symbol s :: rem) -> (Icall_imm s, rem)
|
|
|
|
| (Capply ty, _) -> (Icall_ind, args)
|
1995-07-10 02:48:27 -07:00
|
|
|
| (Cextcall(s, ty, alloc), _) -> (Iextcall(s, alloc), args)
|
1995-06-15 01:17:29 -07:00
|
|
|
| (Cload ty, [arg]) ->
|
|
|
|
let (addr, eloc) = Proc.select_addressing arg in
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iload(Word, addr), [eloc])
|
1995-06-15 01:17:29 -07:00
|
|
|
| (Cloadchunk chunk, [arg]) ->
|
|
|
|
let (addr, eloc) = Proc.select_addressing arg in
|
1995-07-02 09:41:48 -07:00
|
|
|
(Iload(chunk, addr), [eloc])
|
1996-06-04 08:12:08 -07:00
|
|
|
| (Cstore, [arg1; arg2]) ->
|
1995-06-15 01:17:29 -07:00
|
|
|
let (addr, eloc) = Proc.select_addressing arg1 in
|
1996-06-04 08:12:08 -07:00
|
|
|
begin try
|
|
|
|
let (op, newarg2) = Proc.select_store addr arg2 in
|
|
|
|
(op, [newarg2; eloc])
|
|
|
|
with Proc.Use_default ->
|
|
|
|
(Istore(Word, addr), [arg2; eloc])
|
|
|
|
(* Inversion addr/datum in Istore *)
|
|
|
|
end
|
1995-09-25 07:40:58 -07:00
|
|
|
| (Cstorechunk chunk, [arg1; arg2]) ->
|
1995-06-15 01:17:29 -07:00
|
|
|
let (addr, eloc) = Proc.select_addressing arg1 in
|
1995-12-17 08:24:34 -08:00
|
|
|
(Istore(chunk, addr), [arg2; eloc])
|
1995-12-17 07:45:28 -08:00
|
|
|
(* Inversion addr/datum in Istore *)
|
1995-07-02 09:41:48 -07:00
|
|
|
| (Calloc, _) -> (Ialloc 0, args)
|
1995-06-15 01:17:29 -07:00
|
|
|
| (Caddi, _) -> sel_arith_comm Iadd args
|
|
|
|
| (Csubi, _) -> sel_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])
|
|
|
|
else sel_arith_comm Imul args
|
|
|
|
| (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])
|
|
|
|
else sel_arith_comm Imul args
|
1995-06-15 01:17:29 -07:00
|
|
|
| (Cmuli, _) -> sel_arith_comm Imul args
|
|
|
|
| (Cdivi, _) -> sel_arith Idiv args
|
|
|
|
| (Cmodi, _) -> sel_arith_comm Imod args
|
|
|
|
| (Cand, _) -> sel_arith_comm Iand args
|
|
|
|
| (Cor, _) -> sel_arith_comm Ior args
|
|
|
|
| (Cxor, _) -> sel_arith_comm Ixor args
|
1995-09-11 08:15:03 -07:00
|
|
|
| (Clsl, _) -> sel_shift Ilsl args
|
|
|
|
| (Clsr, _) -> sel_shift Ilsr args
|
|
|
|
| (Casr, _) -> sel_shift Iasr args
|
1995-06-15 01:17:29 -07:00
|
|
|
| (Ccmpi comp, _) -> sel_arith_comp (Isigned comp) args
|
|
|
|
| (Cadda, _) -> sel_arith_comm Iadd args
|
|
|
|
| (Csuba, _) -> sel_arith Isub args
|
|
|
|
| (Ccmpa comp, _) -> sel_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)
|
|
|
|
| (Csubf, _) -> (Isubf, args)
|
|
|
|
| (Cmulf, _) -> (Imulf, args)
|
|
|
|
| (Cdivf, _) -> (Idivf, args)
|
|
|
|
| (Cfloatofint, _) -> (Ifloatofint, args)
|
|
|
|
| (Cintoffloat, _) -> (Iintoffloat, args)
|
1995-07-10 02:48:27 -07:00
|
|
|
| (Ccheckbound, _) -> sel_arith Icheckbound args
|
1995-06-15 01:17:29 -07:00
|
|
|
| _ -> fatal_error "Selection.sel_oper"
|
|
|
|
|
|
|
|
and sel_arith_comm op = function
|
1995-07-02 09:41:48 -07:00
|
|
|
[arg; Cconst_int n] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
|
|
|
| [arg; Cconst_pointer n] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
|
|
|
| [Cconst_int n; arg] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
|
|
|
| [Cconst_pointer n; arg] when Proc.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
|
|
|
|
|
|
|
and sel_arith op = function
|
1995-07-02 09:41:48 -07:00
|
|
|
[arg; Cconst_int n] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
|
|
|
| [arg; Cconst_pointer n] when Proc.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
|
|
|
|
1995-09-11 08:15:03 -07:00
|
|
|
and sel_shift op = function
|
|
|
|
[arg; Cconst_int n] when n >= 0 & n < Arch.size_int * 8 ->
|
|
|
|
(Iintop_imm(op, n), [arg])
|
|
|
|
| args ->
|
|
|
|
(Iintop op, args)
|
|
|
|
|
1995-06-15 01:17:29 -07:00
|
|
|
and sel_arith_comp cmp = function
|
1995-07-02 09:41:48 -07:00
|
|
|
[arg; Cconst_int n] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(Icomp cmp, n), [arg])
|
|
|
|
| [arg; Cconst_pointer n] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(Icomp cmp, n), [arg])
|
|
|
|
| [Cconst_int n; arg] when Proc.is_immediate n ->
|
|
|
|
(Iintop_imm(Icomp(swap_intcomp cmp), n), [arg])
|
|
|
|
| [Cconst_pointer n; arg] when Proc.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
|
|
|
|
|
|
|
and swap_intcomp = function
|
|
|
|
Isigned cmp -> Isigned(swap_comparison cmp)
|
|
|
|
| Iunsigned cmp -> Iunsigned(swap_comparison cmp)
|
|
|
|
|
|
|
|
(* Instruction selection for conditionals *)
|
|
|
|
|
|
|
|
let sel_condition = function
|
1995-07-18 10:40:23 -07:00
|
|
|
Cop(Ccmpi cmp, [arg1; Cconst_int n]) when Proc.is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Isigned cmp, n), arg1)
|
1995-07-18 10:40:23 -07:00
|
|
|
| Cop(Ccmpi cmp, [Cconst_int n; arg2]) when Proc.is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Isigned(swap_comparison cmp), n), arg2)
|
|
|
|
| Cop(Ccmpi cmp, args) ->
|
|
|
|
(Iinttest(Isigned cmp), Ctuple args)
|
1995-07-18 10:40:23 -07:00
|
|
|
| Cop(Ccmpa cmp, [arg1; Cconst_pointer n]) when Proc.is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Iunsigned cmp, n), arg1)
|
1995-07-18 10:40:23 -07:00
|
|
|
| Cop(Ccmpa cmp, [Cconst_pointer n; arg2]) when Proc.is_immediate n ->
|
1995-06-15 01:17:29 -07:00
|
|
|
(Iinttest_imm(Iunsigned(swap_comparison cmp), n), arg2)
|
|
|
|
| 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)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* 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
|
|
|
|
|
|
|
|
(* Buffering of instruction sequences *)
|
|
|
|
|
|
|
|
type instruction_sequence = instruction ref
|
|
|
|
|
|
|
|
let new_sequence() = ref dummy_instr
|
|
|
|
|
|
|
|
let insert desc arg res seq =
|
|
|
|
seq := instr_cons desc arg res !seq
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let extract_sequence seq =
|
|
|
|
let rec extract res i =
|
|
|
|
if i == dummy_instr
|
|
|
|
then res
|
|
|
|
else extract (instr_cons i.desc i.arg i.res res) i.next in
|
|
|
|
extract (end_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
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let insert_move src dst seq =
|
|
|
|
if src.stamp <> dst.stamp then
|
|
|
|
insert (Iop Imove) [|src|] [|dst|] seq
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let insert_moves src dst seq =
|
|
|
|
for i = 0 to Array.length src - 1 do
|
|
|
|
insert_move src.(i) dst.(i) seq
|
|
|
|
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 *)
|
|
|
|
|
|
|
|
let insert_move_args arg loc stacksize seq =
|
|
|
|
if stacksize <> 0 then insert (Iop(Istackoffset stacksize)) [||] [||] seq;
|
|
|
|
insert_moves arg loc seq
|
|
|
|
|
|
|
|
let insert_move_results loc res stacksize seq =
|
|
|
|
if stacksize <> 0 then insert(Iop(Istackoffset(-stacksize))) [||] [||] seq;
|
|
|
|
insert_moves loc res seq
|
|
|
|
|
|
|
|
(* "Join" two instruction sequences, making sure they return their results
|
|
|
|
in the same registers. *)
|
|
|
|
|
|
|
|
let join r1 seq1 r2 seq2 =
|
|
|
|
let l1 = Array.length r1 and l2 = Array.length r2 in
|
|
|
|
if l1 = 0 then r2
|
|
|
|
else if l2 = 0 then r1
|
|
|
|
else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = Array.create l1 Reg.dummy in
|
1995-07-02 09:41:48 -07:00
|
|
|
for i = 0 to l1-1 do
|
|
|
|
if String.length r1.(i).name = 0 then begin
|
|
|
|
r.(i) <- r1.(i);
|
|
|
|
insert_move r2.(i) r1.(i) seq2
|
|
|
|
end else if String.length r2.(i).name = 0 then begin
|
|
|
|
r.(i) <- r2.(i);
|
|
|
|
insert_move r1.(i) r2.(i) seq1
|
|
|
|
end else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
r.(i) <- Reg.create r1.(i).typ;
|
1995-07-02 09:41:48 -07:00
|
|
|
insert_move r1.(i) r.(i) seq1;
|
|
|
|
insert_move r2.(i) r.(i) seq2
|
|
|
|
end
|
|
|
|
done;
|
|
|
|
r
|
|
|
|
end
|
|
|
|
|
|
|
|
(* Same, for N branches *)
|
|
|
|
|
|
|
|
let join_array rs =
|
1995-07-13 02:00:54 -07:00
|
|
|
let some_res = ref [||] in
|
1995-07-02 09:41:48 -07:00
|
|
|
for i = 0 to Array.length rs - 1 do
|
|
|
|
let (r, s) = rs.(i) in
|
1995-07-13 02:00:54 -07:00
|
|
|
if Array.length r > 0 then some_res := r
|
1995-07-02 09:41:48 -07:00
|
|
|
done;
|
1995-07-13 02:00:54 -07:00
|
|
|
let size_res = Array.length !some_res in
|
|
|
|
if size_res = 0 then [||] else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let res = Array.create size_res Reg.dummy in
|
1995-07-13 02:00:54 -07:00
|
|
|
for i = 0 to size_res - 1 do
|
1996-04-22 04:15:41 -07:00
|
|
|
res.(i) <- Reg.create (!some_res).(i).typ
|
1995-07-13 02:00:54 -07:00
|
|
|
done;
|
1995-07-02 09:41:48 -07:00
|
|
|
for i = 0 to Array.length rs - 1 do
|
|
|
|
let (r, s) = rs.(i) in
|
1995-07-13 02:00:54 -07:00
|
|
|
if Array.length r > 0 then insert_moves r res s
|
1995-07-02 09:41:48 -07:00
|
|
|
done;
|
1995-07-13 02:00:54 -07:00
|
|
|
res
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-12-10 01:31:57 -08:00
|
|
|
(* Add an Iop opcode.
|
|
|
|
Offer the processor description an opportunity to insert moves
|
|
|
|
before and after the operation, i.e. for two-address
|
|
|
|
instructions, or instructions using dedicated registers. *)
|
|
|
|
|
|
|
|
let insert_op op rs rd seq =
|
|
|
|
try
|
|
|
|
let (rsrc, rdst, move_res) = Proc.pseudoregs_for_operation op rs rd in
|
|
|
|
insert_moves rs rsrc seq;
|
|
|
|
insert (Iop op) rsrc rdst seq;
|
|
|
|
if move_res then begin
|
|
|
|
insert_moves rdst rd seq;
|
|
|
|
rd
|
|
|
|
end else
|
|
|
|
rdst
|
|
|
|
with Proc.Use_default ->
|
|
|
|
(* Assume no constraints on arg and res registers *)
|
|
|
|
insert (Iop op) rs rd seq;
|
|
|
|
rd
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Add the instructions for the given expression
|
|
|
|
at the end of the given sequence *)
|
|
|
|
|
|
|
|
let rec emit_expr env exp seq =
|
|
|
|
match exp with
|
|
|
|
Cconst_int n ->
|
1997-03-04 02:19:51 -08:00
|
|
|
let r = Reg.createv typ_int in
|
|
|
|
insert_op (Iconst_int(Nativeint.from n)) [||] r seq
|
|
|
|
| Cconst_natint n ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = Reg.createv typ_int in
|
1995-12-10 01:31:57 -08:00
|
|
|
insert_op (Iconst_int n) [||] r seq
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cconst_float n ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = Reg.createv typ_float in
|
1995-12-10 01:31:57 -08:00
|
|
|
insert_op (Iconst_float n) [||] r seq
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cconst_symbol n ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = Reg.createv typ_addr in
|
1995-12-10 01:31:57 -08:00
|
|
|
insert_op (Iconst_symbol n) [||] r seq
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cconst_pointer n ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let r = Reg.createv typ_addr in
|
1997-03-04 02:19:51 -08:00
|
|
|
insert_op (Iconst_int(Nativeint.from n)) [||] r seq
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cvar v ->
|
1995-07-02 09:41:48 -07:00
|
|
|
begin try
|
|
|
|
Tbl.find v env
|
|
|
|
with Not_found ->
|
|
|
|
fatal_error("Selection.emit_expr: unbound var " ^ Ident.name v)
|
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Clet(v, e1, e2) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
emit_expr (emit_let env v e1 seq) e2 seq
|
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
|
|
|
|
let r1 = emit_expr env e1 seq in
|
|
|
|
insert_moves r1 rv seq;
|
|
|
|
[||]
|
1996-07-25 06:15:16 -07:00
|
|
|
| Ctuple [] ->
|
|
|
|
[||]
|
1995-07-02 09:41:48 -07:00
|
|
|
| Ctuple exp_list ->
|
|
|
|
let (simple_list, ext_env) = emit_parts_list env exp_list seq in
|
|
|
|
emit_tuple ext_env simple_list seq
|
|
|
|
| Cop(Cproj(ofs, len), [Cop(Cload ty, [arg])]) ->
|
|
|
|
let byte_offset = size_machtype(Array.sub ty 0 ofs) in
|
|
|
|
emit_expr env
|
|
|
|
(Cop(Cload(Array.sub ty ofs len),
|
|
|
|
[Cop(Cadda, [arg; Cconst_int byte_offset])])) seq
|
|
|
|
| Cop(Cproj(ofs, len), [arg]) ->
|
|
|
|
let r = emit_expr env arg seq in
|
|
|
|
Array.sub r ofs len
|
|
|
|
| Cop(Craise, [arg]) ->
|
|
|
|
let r1 = emit_expr env arg seq in
|
|
|
|
let rd = [|Proc.loc_exn_bucket|] in
|
|
|
|
insert (Iop Imove) r1 rd seq;
|
|
|
|
insert Iraise rd [||] seq;
|
|
|
|
[||]
|
1995-07-11 11:03:29 -07:00
|
|
|
| Cop(Ccmpf comp, args) ->
|
|
|
|
emit_expr env (Cifthenelse(exp, Cconst_int 1, Cconst_int 0)) seq
|
1995-07-02 09:41:48 -07:00
|
|
|
| Cop(op, args) ->
|
|
|
|
let (simple_args, env) = emit_parts_list env args seq in
|
|
|
|
let ty = oper_result_type op in
|
|
|
|
let (new_op, new_args) =
|
|
|
|
try
|
|
|
|
Proc.select_oper op simple_args
|
|
|
|
with Proc.Use_default ->
|
|
|
|
sel_operation op simple_args in
|
|
|
|
begin match new_op with
|
|
|
|
Icall_ind ->
|
|
|
|
Proc.contains_calls := true;
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
|
|
|
let rarg = Array.sub r1 1 (Array.length r1 - 1) in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv ty in
|
1995-07-02 09:41:48 -07:00
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments rarg in
|
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
insert_move_args rarg loc_arg stack_ofs seq;
|
|
|
|
insert (Iop Icall_ind) (Array.append [|r1.(0)|] loc_arg) loc_res seq;
|
|
|
|
insert_move_results loc_res rd stack_ofs seq;
|
|
|
|
rd
|
|
|
|
| Icall_imm lbl ->
|
|
|
|
Proc.contains_calls := true;
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv ty in
|
1995-07-02 09:41:48 -07:00
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments r1 in
|
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
insert_move_args r1 loc_arg stack_ofs seq;
|
|
|
|
insert (Iop(Icall_imm lbl)) loc_arg loc_res seq;
|
|
|
|
insert_move_results loc_res rd stack_ofs seq;
|
|
|
|
rd
|
1995-07-10 02:48:27 -07:00
|
|
|
| Iextcall(lbl, alloc) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
Proc.contains_calls := true;
|
1996-05-16 03:39:23 -07:00
|
|
|
if Proc.extcall_use_push then begin
|
|
|
|
let stack_ofs = emit_pushes env new_args seq in
|
|
|
|
let rd = Reg.createv ty in
|
|
|
|
let loc_res = Proc.loc_external_results rd in
|
|
|
|
insert (Iop(Iextcall(lbl, alloc))) [||] loc_res seq;
|
|
|
|
insert_move_results loc_res rd stack_ofs seq;
|
|
|
|
rd
|
|
|
|
end else begin
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
|
|
|
let rd = Reg.createv ty in
|
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_external_arguments r1 in
|
|
|
|
let loc_res = Proc.loc_external_results rd in
|
|
|
|
insert_move_args r1 loc_arg stack_ofs seq;
|
|
|
|
insert (Iop(Iextcall(lbl, alloc))) loc_arg loc_res seq;
|
|
|
|
insert_move_results loc_res rd stack_ofs seq;
|
|
|
|
rd
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Iload(Word, addr) ->
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv ty in
|
1995-12-10 01:31:57 -08:00
|
|
|
insert_op (Iload(Word, addr)) r1 rd seq
|
1995-07-02 09:41:48 -07:00
|
|
|
| Ialloc _ ->
|
|
|
|
Proc.contains_calls := true;
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv typ_addr in
|
1995-07-02 09:41:48 -07:00
|
|
|
let size = size_expr env (Ctuple new_args) in
|
|
|
|
insert (Iop(Ialloc size)) [||] rd seq;
|
1995-12-17 08:24:34 -08:00
|
|
|
emit_stores env new_args seq rd
|
1995-07-02 09:41:48 -07:00
|
|
|
(Arch.offset_addressing Arch.identity_addressing (-Arch.size_int));
|
|
|
|
rd
|
|
|
|
| op ->
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv ty in
|
1995-12-10 01:31:57 -08:00
|
|
|
insert_op op r1 rd seq
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
1995-06-15 01:17:29 -07:00
|
|
|
| Csequence(e1, e2) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
emit_expr env e1 seq;
|
|
|
|
emit_expr env e2 seq
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cifthenelse(econd, eif, eelse) ->
|
|
|
|
let (cond, earg) = sel_condition econd in
|
1995-07-02 09:41:48 -07:00
|
|
|
let rarg = emit_expr env earg seq in
|
|
|
|
let (rif, sif) = emit_sequence env eif in
|
|
|
|
let (relse, selse) = emit_sequence env eelse in
|
|
|
|
let r = join rif sif relse selse in
|
|
|
|
insert (Iifthenelse(cond, extract_sequence sif, extract_sequence selse))
|
|
|
|
rarg [||] seq;
|
|
|
|
r
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cswitch(esel, index, ecases) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let rsel = emit_expr env esel seq in
|
|
|
|
let rscases = Array.map (emit_sequence env) ecases in
|
|
|
|
let r = join_array rscases in
|
|
|
|
insert (Iswitch(index,
|
|
|
|
Array.map (fun (r, s) -> extract_sequence s) rscases))
|
|
|
|
rsel [||] seq;
|
|
|
|
r
|
|
|
|
| Cloop(ebody) ->
|
|
|
|
let (rarg, sbody) = emit_sequence env ebody in
|
|
|
|
insert (Iloop(extract_sequence sbody)) [||] [||] seq;
|
|
|
|
[||]
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ccatch(e1, e2) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let (r1, s1) = emit_sequence env e1 in
|
|
|
|
let (r2, s2) = emit_sequence env e2 in
|
|
|
|
let r = join r1 s1 r2 s2 in
|
|
|
|
insert (Icatch(extract_sequence s1, extract_sequence s2)) [||] [||] seq;
|
|
|
|
r
|
1995-06-15 01:17:29 -07:00
|
|
|
| Cexit ->
|
1995-07-02 09:41:48 -07:00
|
|
|
insert Iexit [||] [||] seq;
|
|
|
|
[||]
|
1995-06-15 01:17:29 -07:00
|
|
|
| Ctrywith(e1, v, e2) ->
|
1995-07-12 07:25:41 -07:00
|
|
|
Proc.contains_calls := true;
|
1995-07-02 09:41:48 -07:00
|
|
|
let (r1, s1) = emit_sequence env e1 in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rv = Reg.createv typ_addr in
|
1995-07-02 09:41:48 -07:00
|
|
|
let (r2, s2) = emit_sequence (Tbl.add v rv env) e2 in
|
|
|
|
let r = join r1 s1 r2 s2 in
|
|
|
|
insert
|
|
|
|
(Itrywith(extract_sequence s1,
|
|
|
|
instr_cons (Iop Imove) [|Proc.loc_exn_bucket|] rv
|
|
|
|
(extract_sequence s2)))
|
|
|
|
[||] [||] seq;
|
|
|
|
r
|
|
|
|
|
|
|
|
and emit_sequence env exp =
|
|
|
|
let seq = new_sequence() in
|
|
|
|
let r = emit_expr env exp seq in
|
|
|
|
(r, seq)
|
|
|
|
|
|
|
|
and emit_let env v e1 seq =
|
|
|
|
let r1 = emit_expr env e1 seq in
|
|
|
|
if all_regs_anonymous r1 then begin
|
|
|
|
name_regs v r1;
|
|
|
|
Tbl.add v r1 env
|
|
|
|
end else begin
|
1996-04-22 04:15:41 -07:00
|
|
|
let rv = Array.create (Array.length r1) Reg.dummy in
|
|
|
|
for i = 0 to Array.length r1 - 1 do rv.(i) <- Reg.create r1.(i).typ done;
|
1995-07-02 09:41:48 -07:00
|
|
|
name_regs v rv;
|
|
|
|
insert_moves r1 rv seq;
|
|
|
|
Tbl.add v rv env
|
|
|
|
end
|
|
|
|
|
|
|
|
and emit_parts env exp seq =
|
1997-02-17 08:52:44 -08:00
|
|
|
if is_simple_expr exp then
|
|
|
|
(exp, env)
|
|
|
|
else begin
|
|
|
|
let r = emit_expr env exp seq in
|
|
|
|
if Array.length r = 0 then
|
|
|
|
(Ctuple [], env)
|
|
|
|
else begin
|
|
|
|
let id = Ident.create "bind" in
|
|
|
|
if all_regs_anonymous r then
|
|
|
|
(Cvar id, Tbl.add id r env)
|
1995-07-02 09:41:48 -07:00
|
|
|
else begin
|
1997-02-17 08:52:44 -08:00
|
|
|
let rv = Array.create (Array.length r) Reg.dummy in
|
|
|
|
for i = 0 to Array.length r - 1 do
|
|
|
|
rv.(i) <- Reg.create r.(i).typ
|
|
|
|
done;
|
|
|
|
insert_moves r rv seq;
|
|
|
|
(Cvar id, Tbl.add id rv env)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
and emit_parts_list env exp_list seq =
|
|
|
|
match exp_list with
|
|
|
|
[] -> ([], env)
|
|
|
|
| exp :: rem ->
|
|
|
|
(* This ensures right-to-left evaluation, consistent with the
|
|
|
|
bytecode compiler *)
|
|
|
|
let (new_rem, new_env) = emit_parts_list env rem seq in
|
|
|
|
let (new_exp, fin_env) = emit_parts new_env exp seq in
|
|
|
|
(new_exp :: new_rem, fin_env)
|
|
|
|
|
|
|
|
and emit_tuple env exp_list seq =
|
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
|
|
|
|
let loc_exp = emit_expr env exp seq in
|
|
|
|
loc_exp :: loc_rem in
|
|
|
|
Array.concat(emit_list exp_list)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1995-12-17 08:24:34 -08:00
|
|
|
and emit_stores env data seq regs_addr addr =
|
1995-07-02 09:41:48 -07:00
|
|
|
let a = ref addr in
|
|
|
|
List.iter
|
|
|
|
(fun e ->
|
1995-06-15 01:17:29 -07:00
|
|
|
try
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Offer the machine description an opportunity to optimize
|
|
|
|
the store, e.g. if constant -> memory or memory -> memory
|
|
|
|
moves are available *)
|
|
|
|
let (op, arg) = Proc.select_store !a e in
|
|
|
|
let r = emit_expr env arg seq in
|
|
|
|
insert (Iop op) (Array.append r regs_addr) [||] seq;
|
|
|
|
a := Arch.offset_addressing !a (size_expr env e)
|
1995-06-15 01:17:29 -07:00
|
|
|
with Proc.Use_default ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let r = emit_expr env e seq in
|
|
|
|
for i = 0 to Array.length r - 1 do
|
1995-12-17 08:24:34 -08:00
|
|
|
insert (Iop(Istore(Word, !a)))
|
1995-07-02 09:41:48 -07:00
|
|
|
(Array.append [|r.(i)|] regs_addr) [||] seq;
|
|
|
|
a := Arch.offset_addressing !a (size_component r.(i).typ)
|
|
|
|
done)
|
|
|
|
data
|
|
|
|
|
1996-05-16 03:39:23 -07:00
|
|
|
and emit_pushes env args seq =
|
|
|
|
match args with
|
|
|
|
[] -> 0
|
|
|
|
| e :: el ->
|
|
|
|
let ofs = emit_pushes env el seq in
|
|
|
|
let (op, arg) = Proc.select_push e in
|
|
|
|
let r = emit_expr env arg seq in
|
|
|
|
insert (Iop op) r [||] seq;
|
|
|
|
ofs + size_expr env e
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Same, but in tail position *)
|
|
|
|
|
|
|
|
let emit_return env exp seq =
|
|
|
|
let r = emit_expr env exp seq in
|
|
|
|
let loc = Proc.loc_results r in
|
|
|
|
insert_moves r loc seq;
|
|
|
|
insert Ireturn loc [||] seq
|
|
|
|
|
|
|
|
let rec emit_tail env exp seq =
|
|
|
|
match exp with
|
|
|
|
Clet(v, e1, e2) ->
|
|
|
|
emit_tail (emit_let env v e1 seq) e2 seq
|
|
|
|
| Cop(Capply ty as op, args) ->
|
|
|
|
let (simple_args, env) = emit_parts_list env args seq in
|
|
|
|
let (new_op, new_args) = sel_operation op simple_args in
|
|
|
|
begin match new_op with
|
|
|
|
Icall_ind ->
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
|
|
|
let rarg = Array.sub r1 1 (Array.length r1 - 1) in
|
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments rarg in
|
1995-07-07 05:07:07 -07:00
|
|
|
if stack_ofs = 0 then begin
|
1995-07-02 09:41:48 -07:00
|
|
|
insert_moves rarg loc_arg seq;
|
|
|
|
insert (Iop Itailcall_ind)
|
|
|
|
(Array.append [|r1.(0)|] loc_arg) [||] seq
|
1995-07-07 05:07:07 -07:00
|
|
|
end else begin
|
|
|
|
Proc.contains_calls := true;
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv ty in
|
1995-07-07 05:07:07 -07:00
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
insert_move_args rarg loc_arg stack_ofs seq;
|
|
|
|
insert (Iop Icall_ind)
|
|
|
|
(Array.append [|r1.(0)|] loc_arg) loc_res seq;
|
|
|
|
insert(Iop(Istackoffset(-stack_ofs))) [||] [||] seq;
|
|
|
|
insert Ireturn loc_res [||] seq
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
|
|
|
| Icall_imm lbl ->
|
|
|
|
let r1 = emit_tuple env new_args seq in
|
|
|
|
let (loc_arg, stack_ofs) = Proc.loc_arguments r1 in
|
1995-07-07 05:07:07 -07:00
|
|
|
if stack_ofs = 0 then begin
|
1995-07-02 09:41:48 -07:00
|
|
|
insert_moves r1 loc_arg seq;
|
|
|
|
insert (Iop(Itailcall_imm lbl)) loc_arg [||] seq
|
1995-07-07 05:07:07 -07:00
|
|
|
end else begin
|
|
|
|
Proc.contains_calls := true;
|
1996-04-22 04:15:41 -07:00
|
|
|
let rd = Reg.createv ty in
|
1995-07-07 05:07:07 -07:00
|
|
|
let loc_res = Proc.loc_results rd in
|
|
|
|
insert_move_args r1 loc_arg stack_ofs seq;
|
|
|
|
insert (Iop(Icall_imm lbl)) loc_arg loc_res seq;
|
|
|
|
insert(Iop(Istackoffset(-stack_ofs))) [||] [||] seq;
|
|
|
|
insert Ireturn loc_res [||] seq
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
|
|
|
| _ -> fatal_error "Selection.emit_tail"
|
|
|
|
end
|
|
|
|
| Cop(Craise, [e1]) ->
|
|
|
|
let r1 = emit_expr env e1 seq in
|
|
|
|
let rd = [|Proc.loc_exn_bucket|] in
|
|
|
|
insert (Iop Imove) r1 rd seq;
|
|
|
|
insert Iraise rd [||] seq
|
|
|
|
| Csequence(e1, e2) ->
|
|
|
|
emit_expr env e1 seq;
|
|
|
|
emit_tail env e2 seq
|
|
|
|
| Cifthenelse(econd, eif, eelse) ->
|
|
|
|
let (cond, earg) = sel_condition econd in
|
|
|
|
let rarg = emit_expr env earg seq in
|
|
|
|
insert (Iifthenelse(cond, emit_tail_sequence env eif,
|
|
|
|
emit_tail_sequence env eelse))
|
|
|
|
rarg [||] seq
|
|
|
|
| Cswitch(esel, index, ecases) ->
|
|
|
|
let rsel = emit_expr env esel seq in
|
|
|
|
insert (Iswitch(index, Array.map (emit_tail_sequence env) ecases))
|
|
|
|
rsel [||] seq
|
|
|
|
| Ccatch(e1, e2) ->
|
|
|
|
insert (Icatch(emit_tail_sequence env e1, emit_tail_sequence env e2))
|
|
|
|
[||] [||] seq
|
|
|
|
| Cexit ->
|
|
|
|
insert Iexit [||] [||] seq
|
1995-07-24 05:44:17 -07:00
|
|
|
| Ctrywith(e1, v, e2) ->
|
|
|
|
Proc.contains_calls := true;
|
|
|
|
let (r1, s1) = emit_sequence env e1 in
|
1996-04-22 04:15:41 -07:00
|
|
|
let rv = Reg.createv typ_addr in
|
1995-07-24 05:44:17 -07:00
|
|
|
let s2 = emit_tail_sequence (Tbl.add v rv env) e2 in
|
|
|
|
let loc = Proc.loc_results r1 in
|
|
|
|
insert
|
|
|
|
(Itrywith(extract_sequence s1,
|
|
|
|
instr_cons (Iop Imove) [|Proc.loc_exn_bucket|] rv s2))
|
|
|
|
[||] [||] seq;
|
|
|
|
insert_moves r1 loc seq;
|
|
|
|
insert Ireturn loc [||] seq
|
1995-07-02 09:41:48 -07:00
|
|
|
| _ ->
|
|
|
|
emit_return env exp seq
|
|
|
|
|
|
|
|
and emit_tail_sequence env exp =
|
|
|
|
let seq = new_sequence() in
|
|
|
|
emit_tail env exp seq;
|
|
|
|
extract_sequence seq
|
|
|
|
|
|
|
|
(* Sequentialization of a function definition *)
|
|
|
|
|
|
|
|
let fundecl f =
|
|
|
|
Proc.contains_calls := false;
|
|
|
|
let rargs =
|
|
|
|
List.map
|
1996-04-22 04:15:41 -07:00
|
|
|
(fun (id, ty) -> let r = Reg.createv 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
|
|
|
|
let seq = new_sequence() in
|
|
|
|
insert_moves loc_arg rarg seq;
|
|
|
|
emit_tail env f.Cmm.fun_body seq;
|
|
|
|
{ fun_name = f.Cmm.fun_name;
|
|
|
|
fun_args = loc_arg;
|
|
|
|
fun_body = extract_sequence seq;
|
|
|
|
fun_fast = f.Cmm.fun_fast }
|