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-06-15 01:17:29 -07:00
|
|
|
(* Insert load/stores for pseudoregs that got assigned to stack locations.
|
|
|
|
Insert moves to comply with calling conventions, etc. *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
|
|
|
|
let redo_regalloc = ref false
|
|
|
|
|
|
|
|
let access_stack r =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length r - 1 do
|
|
|
|
match r.(i).loc with Stack _ -> raise Exit | _ -> ()
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit ->
|
|
|
|
true
|
|
|
|
|
|
|
|
let makereg r =
|
|
|
|
match r.loc with
|
|
|
|
Unknown -> fatal_error "Reload.makereg"
|
|
|
|
| Reg _ -> r
|
1996-05-16 12:39:26 -07:00
|
|
|
| Stack _ ->
|
|
|
|
if Proc.num_available_registers.(Proc.register_class r) = 0
|
|
|
|
then r
|
|
|
|
else begin redo_regalloc := true; Reg.clone r end
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
let makeregs rv =
|
|
|
|
let n = Array.length rv in
|
1996-04-22 04:15:41 -07:00
|
|
|
let newv = Array.create n Reg.dummy in
|
1995-06-15 01:17:29 -07:00
|
|
|
for i = 0 to n-1 do newv.(i) <- makereg rv.(i) done;
|
|
|
|
newv
|
|
|
|
|
|
|
|
let insert_move src dst next =
|
|
|
|
if src.loc = dst.loc
|
|
|
|
then next
|
|
|
|
else instr_cons (Iop Imove) [|src|] [|dst|] next
|
|
|
|
|
|
|
|
let insert_moves src dst next =
|
|
|
|
let rec insmoves i =
|
|
|
|
if i >= Array.length src
|
|
|
|
then next
|
|
|
|
else insert_move src.(i) dst.(i) (insmoves (i+1))
|
|
|
|
in insmoves 0
|
|
|
|
|
|
|
|
let rec reload i =
|
|
|
|
match i.desc with
|
|
|
|
Iend | Ireturn | Iop Itailcall_ind | Iop(Itailcall_imm _) | Iraise -> i
|
1995-07-10 02:48:27 -07:00
|
|
|
| Iop(Icall_ind | Icall_imm _ | Iextcall(_, _)) ->
|
1995-07-07 05:11:24 -07:00
|
|
|
(* Don't do anything, the arguments and results are already at
|
|
|
|
the correct position (e.g. on stack for some arguments). *)
|
1995-07-07 09:14:06 -07:00
|
|
|
instr_cons_live i.desc i.arg i.res i.live (reload i.next)
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iop op ->
|
|
|
|
(* Let the machine description tell us whether some arguments / results
|
|
|
|
can reside on the stack *)
|
|
|
|
let (newarg, newres) =
|
|
|
|
try
|
|
|
|
Proc.reload_operation makereg op i.arg i.res
|
|
|
|
with Proc.Use_default ->
|
|
|
|
(* By default, assume that arguments and results must reside
|
1995-12-10 01:31:57 -08:00
|
|
|
in hardware registers. For moves, allow one arg or one
|
|
|
|
res to be stack-allocated, but do something for
|
|
|
|
stack-to-stack moves *)
|
|
|
|
match op with
|
|
|
|
Imove | Ireload | Ispill ->
|
|
|
|
begin match i.arg.(0), i.res.(0) with
|
|
|
|
{loc = Stack s1}, {loc = Stack s2} when s1 <> s2 ->
|
|
|
|
([| makereg i.arg.(0) |], i.res)
|
|
|
|
| _ ->
|
|
|
|
(i.arg, i.res)
|
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
(makeregs i.arg, makeregs i.res) in
|
1995-06-15 01:17:29 -07:00
|
|
|
insert_moves i.arg newarg
|
1995-07-02 09:41:48 -07:00
|
|
|
(instr_cons_live i.desc newarg newres i.live
|
1995-06-15 01:17:29 -07:00
|
|
|
(insert_moves newres i.res
|
|
|
|
(reload i.next)))
|
|
|
|
| Iifthenelse(tst, ifso, ifnot) ->
|
|
|
|
(* Let the machine description tell us whether some arguments / results
|
|
|
|
can reside on the stack *)
|
|
|
|
let newarg =
|
|
|
|
try
|
|
|
|
Proc.reload_test makereg tst i.arg
|
|
|
|
with Proc.Use_default ->
|
|
|
|
makeregs i.arg in
|
|
|
|
insert_moves i.arg newarg
|
|
|
|
(instr_cons (Iifthenelse(tst, reload ifso, reload ifnot)) newarg [||]
|
|
|
|
(reload i.next))
|
|
|
|
| Iswitch(index, cases) ->
|
|
|
|
let newarg = makeregs i.arg in
|
|
|
|
insert_moves i.arg newarg
|
|
|
|
(instr_cons (Iswitch(index, Array.map reload cases)) newarg [||]
|
|
|
|
(reload i.next))
|
|
|
|
| Iloop body ->
|
|
|
|
instr_cons (Iloop(reload body)) [||] [||] (reload i.next)
|
|
|
|
| Icatch(body, handler) ->
|
|
|
|
instr_cons (Icatch(reload body, reload handler)) [||] [||]
|
|
|
|
(reload i.next)
|
|
|
|
| Iexit ->
|
|
|
|
instr_cons Iexit [||] [||] dummy_instr
|
|
|
|
| Itrywith(body, handler) ->
|
|
|
|
instr_cons (Itrywith(reload body, reload handler)) [||] [||]
|
|
|
|
(reload i.next)
|
|
|
|
|
|
|
|
let fundecl f =
|
|
|
|
redo_regalloc := false;
|
|
|
|
let new_body = reload f.fun_body in
|
1995-07-02 09:41:48 -07:00
|
|
|
({fun_name = f.fun_name; fun_args = f.fun_args;
|
|
|
|
fun_body = new_body; fun_fast = f.fun_fast},
|
1995-06-15 01:17:29 -07:00
|
|
|
!redo_regalloc)
|
|
|
|
|