1997-07-24 04:49:12 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Objective Caml *)
|
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* Copyright 1997 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. *)
|
1997-07-24 04:49:12 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
(* Instruction selection for the Intel x86 *)
|
|
|
|
|
|
|
|
open Misc
|
|
|
|
open Arch
|
|
|
|
open Proc
|
|
|
|
open Cmm
|
|
|
|
open Reg
|
|
|
|
open Mach
|
|
|
|
|
|
|
|
(* Auxiliary for recognizing addressing modes *)
|
|
|
|
|
|
|
|
type addressing_expr =
|
|
|
|
Asymbol of string
|
|
|
|
| Alinear of expression
|
|
|
|
| Aadd of expression * expression
|
|
|
|
| Ascale of expression * int
|
|
|
|
| Ascaledadd of expression * expression * int
|
|
|
|
|
|
|
|
let rec select_addr exp =
|
|
|
|
match exp with
|
|
|
|
Cconst_symbol s ->
|
|
|
|
(Asymbol s, 0)
|
|
|
|
| Cop((Caddi | Cadda), [arg; Cconst_int m]) ->
|
|
|
|
let (a, n) = select_addr arg in (a, n + m)
|
|
|
|
| Cop((Csubi | Csuba), [arg; Cconst_int m]) ->
|
|
|
|
let (a, n) = select_addr arg in (a, n - m)
|
|
|
|
| Cop((Caddi | Cadda), [Cconst_int m; arg]) ->
|
|
|
|
let (a, n) = select_addr arg in (a, n + m)
|
|
|
|
| Cop(Clsl, [arg; Cconst_int(1|2|3 as shift)]) ->
|
|
|
|
begin match select_addr arg with
|
|
|
|
(Alinear e, n) -> (Ascale(e, 1 lsl shift), n lsl shift)
|
|
|
|
| _ -> (Alinear exp, 0)
|
|
|
|
end
|
|
|
|
| Cop(Cmuli, [arg; Cconst_int(2|4|8 as mult)]) ->
|
|
|
|
begin match select_addr arg with
|
|
|
|
(Alinear e, n) -> (Ascale(e, mult), n * mult)
|
|
|
|
| _ -> (Alinear exp, 0)
|
|
|
|
end
|
|
|
|
| Cop(Cmuli, [Cconst_int(2|4|8 as mult); arg]) ->
|
|
|
|
begin match select_addr arg with
|
|
|
|
(Alinear e, n) -> (Ascale(e, mult), n * mult)
|
|
|
|
| _ -> (Alinear exp, 0)
|
|
|
|
end
|
|
|
|
| Cop((Caddi | Cadda), [arg1; arg2]) ->
|
|
|
|
begin match (select_addr arg1, select_addr arg2) with
|
|
|
|
((Alinear e1, n1), (Alinear e2, n2)) ->
|
|
|
|
(Aadd(e1, e2), n1 + n2)
|
|
|
|
| ((Alinear e1, n1), (Ascale(e2, scale), n2)) ->
|
|
|
|
(Ascaledadd(e1, e2, scale), n1 + n2)
|
|
|
|
| ((Ascale(e1, scale), n1), (Alinear e2, n2)) ->
|
|
|
|
(Ascaledadd(e2, e1, scale), n1 + n2)
|
|
|
|
| (_, (Ascale(e2, scale), n2)) ->
|
|
|
|
(Ascaledadd(arg1, e2, scale), n2)
|
|
|
|
| ((Ascale(e1, scale), n1), _) ->
|
|
|
|
(Ascaledadd(arg2, e1, scale), n1)
|
|
|
|
| _ ->
|
|
|
|
(Aadd(arg1, arg2), 0)
|
|
|
|
end
|
|
|
|
| arg ->
|
|
|
|
(Alinear arg, 0)
|
|
|
|
|
|
|
|
(* Estimate number of float temporaries needed to evaluate expression
|
|
|
|
(Ershov's algorithm) *)
|
|
|
|
|
|
|
|
let rec float_needs = function
|
|
|
|
Cop((Caddf | Csubf | Cmulf | Cdivf), [arg1; arg2]) ->
|
|
|
|
let n1 = float_needs arg1 in
|
|
|
|
let n2 = float_needs arg2 in
|
|
|
|
if n1 = n2 then 1 + n1 else if n1 > n2 then n1 else n2
|
|
|
|
| _ ->
|
|
|
|
1
|
|
|
|
|
|
|
|
(* Special constraints on operand and result registers *)
|
|
|
|
|
|
|
|
exception Use_default
|
|
|
|
|
|
|
|
let eax = phys_reg 0
|
|
|
|
let ecx = phys_reg 2
|
|
|
|
let edx = phys_reg 3
|
|
|
|
let tos = phys_reg 100
|
|
|
|
|
|
|
|
let pseudoregs_for_operation op arg res =
|
|
|
|
match op with
|
|
|
|
(* Two-address binary operations *)
|
|
|
|
Iintop(Iadd|Isub|Imul|Iand|Ior|Ixor) ->
|
|
|
|
([|res.(0); arg.(1)|], res, false)
|
|
|
|
(* Two-address unary operations *)
|
|
|
|
| Iintop_imm((Iadd|Isub|Imul|Idiv|Iand|Ior|Ixor|Ilsl|Ilsr|Iasr), _) ->
|
|
|
|
(res, res, false)
|
|
|
|
(* For shifts with variable shift count, second arg must be in ecx *)
|
|
|
|
| Iintop(Ilsl|Ilsr|Iasr) ->
|
|
|
|
([|res.(0); ecx|], res, false)
|
|
|
|
(* For div and mod, first arg must be in eax, edx is clobbered,
|
|
|
|
and result is in eax or edx respectively.
|
|
|
|
Keep it simple, just force second argument in ecx. *)
|
|
|
|
| Iintop(Idiv) ->
|
|
|
|
([| eax; ecx |], [| eax |], true)
|
|
|
|
| Iintop(Imod) ->
|
|
|
|
([| eax; ecx |], [| edx |], true)
|
|
|
|
(* For mod with immediate operand, arg must not be in eax.
|
|
|
|
Keep it simple, force it in edx. *)
|
|
|
|
| Iintop_imm(Imod, _) ->
|
|
|
|
([| edx |], [| edx |], true)
|
2000-02-04 04:43:18 -08:00
|
|
|
(* For floating-point operations and floating-point loads,
|
|
|
|
the result is always left at the top of the floating-point stack *)
|
1997-07-24 04:49:12 -07:00
|
|
|
| Iconst_float _ | Inegf | Iabsf | Iaddf | Isubf | Imulf | Idivf
|
2000-03-10 06:31:06 -08:00
|
|
|
| Ifloatofint | Iload((Single | Double | Double_u), _)
|
|
|
|
| Ispecific(Isubfrev | Idivfrev | Ifloatarithmem(_, _, _)) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
(arg, [| tos |], false) (* don't move it immediately *)
|
|
|
|
(* For storing a byte, the argument must be in eax...edx.
|
2000-02-04 04:43:18 -08:00
|
|
|
(But for a short, any reg will do!)
|
|
|
|
Keep it simple, just force the argument to be in edx. *)
|
|
|
|
| Istore((Byte_unsigned | Byte_signed), addr) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let newarg = Array.copy arg in
|
|
|
|
newarg.(0) <- edx;
|
|
|
|
(newarg, res, false)
|
|
|
|
(* Other instructions are regular *)
|
|
|
|
| _ -> raise Use_default
|
|
|
|
|
2000-03-10 06:31:06 -08:00
|
|
|
let chunk_double = function
|
|
|
|
Single -> false
|
|
|
|
| Double -> true
|
|
|
|
| Double_u -> true
|
|
|
|
| _ -> assert false
|
|
|
|
|
1997-07-24 04:49:12 -07:00
|
|
|
(* The selector class *)
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
class selector = object (self)
|
1997-07-24 04:49:12 -07:00
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
inherit Selectgen.selector_generic as super
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
method is_immediate (n : int) = true
|
|
|
|
|
|
|
|
method select_addressing exp =
|
|
|
|
match select_addr exp with
|
|
|
|
(Asymbol s, d) ->
|
|
|
|
(Ibased(s, d), Ctuple [])
|
|
|
|
| (Alinear e, d) ->
|
|
|
|
(Iindexed d, e)
|
|
|
|
| (Aadd(e1, e2), d) ->
|
|
|
|
(Iindexed2 d, Ctuple[e1; e2])
|
|
|
|
| (Ascale(e, scale), d) ->
|
|
|
|
(Iscaled(scale, d), e)
|
|
|
|
| (Ascaledadd(e1, e2, scale), d) ->
|
|
|
|
(Iindexed2scaled(scale, d), Ctuple[e1; e2])
|
|
|
|
|
|
|
|
method select_store addr exp =
|
|
|
|
match exp with
|
1999-02-04 02:30:31 -08:00
|
|
|
Cconst_int n ->
|
2000-02-11 07:09:27 -08:00
|
|
|
(Ispecific(Istore_int(Nativeint.of_int n, addr)), Ctuple [])
|
1999-02-04 02:30:31 -08:00
|
|
|
| Cconst_natint n ->
|
|
|
|
(Ispecific(Istore_int(n, addr)), Ctuple [])
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cconst_pointer n ->
|
|
|
|
(Ispecific(Istore_int(Nativeint.of_int n, addr)), Ctuple [])
|
|
|
|
| Cconst_natpointer n ->
|
|
|
|
(Ispecific(Istore_int(n, addr)), Ctuple [])
|
1999-02-04 02:30:31 -08:00
|
|
|
| Cconst_symbol s ->
|
|
|
|
(Ispecific(Istore_symbol(s, addr)), Ctuple [])
|
|
|
|
| _ ->
|
|
|
|
super#select_store addr exp
|
1997-07-24 04:49:12 -07:00
|
|
|
|
|
|
|
method select_operation op args =
|
|
|
|
match op with
|
|
|
|
(* Recognize the LEA instruction *)
|
|
|
|
Caddi | Cadda | Csubi | Csuba ->
|
|
|
|
begin match self#select_addressing (Cop(op, args)) with
|
|
|
|
(Iindexed d, _) -> super#select_operation op args
|
|
|
|
| (Iindexed2 0, _) -> super#select_operation op args
|
|
|
|
| (addr, arg) -> (Ispecific(Ilea addr), [arg])
|
|
|
|
end
|
|
|
|
(* Recognize (x / cst) and (x % cst) only if cst is a power of 2. *)
|
|
|
|
| Cdivi ->
|
|
|
|
begin match args with
|
|
|
|
[arg1; Cconst_int n] when n = 1 lsl (Misc.log2 n) ->
|
|
|
|
(Iintop_imm(Idiv, n), [arg1])
|
|
|
|
| _ -> (Iintop Idiv, args)
|
|
|
|
end
|
|
|
|
| Cmodi ->
|
|
|
|
begin match args with
|
|
|
|
[arg1; Cconst_int n] when n = 1 lsl (Misc.log2 n) ->
|
|
|
|
(Iintop_imm(Imod, n), [arg1])
|
|
|
|
| _ -> (Iintop Imod, args)
|
|
|
|
end
|
|
|
|
(* Recognize float arithmetic with memory.
|
|
|
|
In passing, apply Ershov's algorithm to reduce stack usage *)
|
|
|
|
| Caddf ->
|
|
|
|
self#select_floatarith Iaddf Iaddf Ifloatadd Ifloatadd args
|
|
|
|
| Csubf ->
|
|
|
|
self#select_floatarith Isubf (Ispecific Isubfrev) Ifloatsub Ifloatsubrev args
|
|
|
|
| Cmulf ->
|
|
|
|
self#select_floatarith Imulf Imulf Ifloatmul Ifloatmul args
|
|
|
|
| Cdivf ->
|
|
|
|
self#select_floatarith Idivf (Ispecific Idivfrev) Ifloatdiv Ifloatdivrev args
|
1997-11-14 03:02:05 -08:00
|
|
|
(* Recognize store instructions *)
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cstore Word ->
|
1997-11-14 03:02:05 -08:00
|
|
|
begin match args with
|
|
|
|
[loc; Cop(Caddi, [Cop(Cload _, [loc']); Cconst_int n])]
|
|
|
|
when loc = loc' ->
|
|
|
|
let (addr, arg) = self#select_addressing loc in
|
|
|
|
(Ispecific(Ioffset_loc(n, addr)), [arg])
|
|
|
|
| _ ->
|
|
|
|
super#select_operation op args
|
|
|
|
end
|
1997-07-24 04:49:12 -07:00
|
|
|
| _ -> super#select_operation op args
|
|
|
|
|
|
|
|
(* Recognize float arithmetic with mem *)
|
|
|
|
|
|
|
|
method select_floatarith regular_op reversed_op mem_op mem_rev_op args =
|
|
|
|
match args with
|
2000-03-10 06:31:06 -08:00
|
|
|
[arg1; Cop(Cload chunk, [loc2])] ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (addr, arg2) = self#select_addressing loc2 in
|
2000-03-10 06:31:06 -08:00
|
|
|
(Ispecific(Ifloatarithmem(chunk_double chunk, mem_op, addr)),
|
|
|
|
[arg1; arg2])
|
|
|
|
| [Cop(Cload chunk, [loc1]); arg2] ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (addr, arg1) = self#select_addressing loc1 in
|
2000-03-10 06:31:06 -08:00
|
|
|
(Ispecific(Ifloatarithmem(chunk_double chunk, mem_rev_op, addr)),
|
|
|
|
[arg2; arg1])
|
1997-07-24 04:49:12 -07:00
|
|
|
| [arg1; arg2] ->
|
|
|
|
(* Evaluate bigger subexpression first to minimize stack usage.
|
|
|
|
Because of right-to-left evaluation, rightmost arg is evaluated
|
|
|
|
first *)
|
|
|
|
if float_needs arg1 <= float_needs arg2
|
|
|
|
then (regular_op, [arg1; arg2])
|
|
|
|
else (reversed_op, [arg2; arg1])
|
|
|
|
| _ ->
|
|
|
|
fatal_error "Proc_i386: select_floatarith"
|
|
|
|
|
|
|
|
(* Deal with register constraints *)
|
|
|
|
|
|
|
|
method insert_op op rs rd =
|
|
|
|
try
|
|
|
|
let (rsrc, rdst, move_res) = pseudoregs_for_operation op rs rd in
|
|
|
|
self#insert_moves rs rsrc;
|
|
|
|
self#insert (Iop op) rsrc rdst;
|
|
|
|
if move_res then begin
|
|
|
|
self#insert_moves rdst rd;
|
|
|
|
rd
|
|
|
|
end else
|
|
|
|
rdst
|
|
|
|
with Use_default ->
|
|
|
|
super#insert_op op rs rd
|
|
|
|
|
|
|
|
(* Selection of push instructions for external calls *)
|
|
|
|
|
|
|
|
method select_push exp =
|
|
|
|
match exp with
|
2000-02-11 07:09:27 -08:00
|
|
|
Cconst_int n -> (Ispecific(Ipush_int(Nativeint.of_int n)), Ctuple [])
|
1999-02-04 02:30:31 -08:00
|
|
|
| Cconst_natint n -> (Ispecific(Ipush_int n), Ctuple [])
|
2000-03-17 05:24:17 -08:00
|
|
|
| Cconst_pointer n -> (Ispecific(Ipush_int(Nativeint.of_int n)), Ctuple [])
|
|
|
|
| Cconst_natpointer n -> (Ispecific(Ipush_int n), Ctuple [])
|
1997-07-24 04:49:12 -07:00
|
|
|
| Cconst_symbol s -> (Ispecific(Ipush_symbol s), Ctuple [])
|
2000-02-04 04:43:18 -08:00
|
|
|
| Cop(Cload Word, [loc]) ->
|
1997-07-24 04:49:12 -07:00
|
|
|
let (addr, arg) = self#select_addressing loc in
|
|
|
|
(Ispecific(Ipush_load addr), arg)
|
2000-03-10 06:31:06 -08:00
|
|
|
| Cop(Cload Double_u, [loc]) ->
|
2000-02-04 04:43:18 -08:00
|
|
|
let (addr, arg) = self#select_addressing loc in
|
|
|
|
(Ispecific(Ipush_load_float addr), arg)
|
1997-07-24 04:49:12 -07:00
|
|
|
| _ -> (Ispecific(Ipush), exp)
|
|
|
|
|
|
|
|
method emit_extcall_args env args =
|
|
|
|
let rec emit_pushes = function
|
|
|
|
[] -> 0
|
|
|
|
| e :: el ->
|
|
|
|
let ofs = emit_pushes el in
|
|
|
|
let (op, arg) = self#select_push e in
|
|
|
|
let r = self#emit_expr env arg in
|
|
|
|
self#insert (Iop op) r [||];
|
|
|
|
ofs + Selectgen.size_expr env e
|
|
|
|
in ([||], emit_pushes args)
|
|
|
|
|
|
|
|
end
|
|
|
|
|
1998-06-24 12:22:26 -07:00
|
|
|
let fundecl f = (new selector)#emit_fundecl f
|
1997-07-24 04:49:12 -07:00
|
|
|
|