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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* Insert load/stores for pseudoregs that got assigned to stack locations. *)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
|
|
|
|
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 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
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
class reload_generic = object (self)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
val mutable redo_regalloc = false
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
method makereg r =
|
|
|
|
match r.loc with
|
|
|
|
Unknown -> fatal_error "Reload.makereg"
|
|
|
|
| Reg _ -> r
|
|
|
|
| Stack _ ->
|
|
|
|
redo_regalloc <- true;
|
|
|
|
let newr = Reg.clone r in
|
|
|
|
(* Strongly discourage spilling this register *)
|
|
|
|
newr.spill_cost <- 100000;
|
|
|
|
newr
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private makeregs rv =
|
1997-07-24 04:49:12 -07:00
|
|
|
let n = Array.length rv in
|
|
|
|
let newv = Array.create n Reg.dummy in
|
|
|
|
for i = 0 to n-1 do newv.(i) <- self#makereg rv.(i) done;
|
|
|
|
newv
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private makereg1 rv =
|
1997-07-24 04:49:12 -07:00
|
|
|
let newv = Array.copy rv in
|
|
|
|
newv.(0) <- self#makereg rv.(0);
|
|
|
|
newv
|
|
|
|
|
|
|
|
method reload_operation op arg res =
|
|
|
|
(* By default, assume that arguments and results must reside
|
|
|
|
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 arg.(0), res.(0) with
|
|
|
|
{loc = Stack s1}, {loc = Stack s2} when s1 <> s2 ->
|
|
|
|
([| self#makereg arg.(0) |], res)
|
|
|
|
| _ ->
|
|
|
|
(arg, res)
|
|
|
|
end
|
|
|
|
| _ ->
|
|
|
|
(self#makeregs arg, self#makeregs res)
|
1996-09-11 08:59:11 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method reload_test tst args =
|
|
|
|
self#makeregs args
|
1996-09-18 05:45:15 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
method private reload i =
|
1995-06-15 01:17:29 -07:00
|
|
|
match i.desc with
|
1996-09-18 05:45:15 -07:00
|
|
|
(* For function calls, returns, etc: the arguments and results are
|
|
|
|
already at the correct position (e.g. on stack for some arguments).
|
|
|
|
However, something needs to be done for the function pointer in
|
|
|
|
indirect calls. *)
|
2013-10-14 07:33:27 -07:00
|
|
|
Iend | Ireturn | Iop(Itailcall_imm _) | Iraise _ -> i
|
1996-09-18 05:45:15 -07:00
|
|
|
| Iop(Itailcall_ind) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let newarg = self#makereg1 i.arg in
|
1996-09-18 05:45:15 -07:00
|
|
|
insert_moves i.arg newarg
|
2007-01-29 04:11:18 -08:00
|
|
|
{i with arg = newarg}
|
|
|
|
| Iop(Icall_imm _ | Iextcall _) ->
|
|
|
|
{i with next = self#reload i.next}
|
1996-09-18 05:45:15 -07:00
|
|
|
| Iop(Icall_ind) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let newarg = self#makereg1 i.arg in
|
1996-09-18 05:45:15 -07:00
|
|
|
insert_moves i.arg newarg
|
2007-01-29 04:11:18 -08:00
|
|
|
{i with arg = newarg; next = self#reload i.next}
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iop op ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (newarg, newres) = self#reload_operation op i.arg i.res in
|
1995-06-15 01:17:29 -07:00
|
|
|
insert_moves i.arg newarg
|
2007-01-29 04:11:18 -08:00
|
|
|
{i with arg = newarg; res = newres; next =
|
1995-06-15 01:17:29 -07:00
|
|
|
(insert_moves newres i.res
|
2007-01-29 04:11:18 -08:00
|
|
|
(self#reload i.next))}
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iifthenelse(tst, ifso, ifnot) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let newarg = self#reload_test tst i.arg in
|
2010-01-22 04:48:24 -08:00
|
|
|
insert_moves i.arg newarg
|
1997-07-24 04:49:12 -07:00
|
|
|
(instr_cons
|
|
|
|
(Iifthenelse(tst, self#reload ifso, self#reload ifnot)) newarg [||]
|
|
|
|
(self#reload i.next))
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iswitch(index, cases) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let newarg = self#makeregs i.arg in
|
2010-01-22 04:48:24 -08:00
|
|
|
insert_moves i.arg newarg
|
1998-06-24 12:22:26 -07:00
|
|
|
(instr_cons (Iswitch(index, Array.map (self#reload) cases)) newarg [||]
|
1997-07-24 04:49:12 -07:00
|
|
|
(self#reload i.next))
|
1995-06-15 01:17:29 -07:00
|
|
|
| Iloop body ->
|
1997-07-24 04:49:12 -07:00
|
|
|
instr_cons (Iloop(self#reload body)) [||] [||] (self#reload i.next)
|
2000-08-11 12:50:59 -07:00
|
|
|
| Icatch(nfail, body, handler) ->
|
|
|
|
instr_cons
|
|
|
|
(Icatch(nfail, self#reload body, self#reload handler)) [||] [||]
|
1997-07-24 04:49:12 -07:00
|
|
|
(self#reload i.next)
|
2000-08-11 12:50:59 -07:00
|
|
|
| Iexit i ->
|
|
|
|
instr_cons (Iexit i) [||] [||] dummy_instr
|
1995-06-15 01:17:29 -07:00
|
|
|
| Itrywith(body, handler) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
instr_cons (Itrywith(self#reload body, self#reload handler)) [||] [||]
|
|
|
|
(self#reload i.next)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
method fundecl f =
|
|
|
|
redo_regalloc <- false;
|
|
|
|
let new_body = self#reload f.fun_body in
|
1995-07-02 09:41:48 -07:00
|
|
|
({fun_name = f.fun_name; fun_args = f.fun_args;
|
2012-02-21 09:41:02 -08:00
|
|
|
fun_body = new_body; fun_fast = f.fun_fast;
|
|
|
|
fun_dbg = f.fun_dbg},
|
1997-07-24 04:49:12 -07:00
|
|
|
redo_regalloc)
|
1995-06-15 01:17:29 -07:00
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
end
|