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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Introduction of closures, uncurrying, recognition of direct calls *)
|
|
|
|
|
|
|
|
open Misc
|
1995-11-09 05:21:49 -08:00
|
|
|
open Asttypes
|
1997-02-16 09:20:11 -08:00
|
|
|
open Primitive
|
1995-07-02 09:41:48 -07:00
|
|
|
open Lambda
|
2001-02-19 12:15:42 -08:00
|
|
|
open Switch
|
1995-07-02 09:41:48 -07:00
|
|
|
open Clambda
|
|
|
|
|
|
|
|
(* Auxiliaries for compiling functions *)
|
|
|
|
|
|
|
|
let rec split_list n l =
|
|
|
|
if n <= 0 then ([], l) else begin
|
|
|
|
match l with
|
|
|
|
[] -> fatal_error "Closure.split_list"
|
|
|
|
| a::l -> let (l1, l2) = split_list (n-1) l in (a::l1, l2)
|
|
|
|
end
|
|
|
|
|
|
|
|
let rec build_closure_env env_param pos = function
|
|
|
|
[] -> Tbl.empty
|
|
|
|
| id :: rem ->
|
2007-01-29 04:11:18 -08:00
|
|
|
Tbl.add id (Uprim(Pfield pos, [Uvar env_param], Debuginfo.none))
|
1995-07-02 09:41:48 -07:00
|
|
|
(build_closure_env env_param (pos+1) rem)
|
|
|
|
|
2006-01-04 08:55:50 -08:00
|
|
|
(* Auxiliary for accessing globals. We change the name of the global
|
|
|
|
to the name of the corresponding asm symbol. This is done here
|
|
|
|
and no longer in Cmmgen so that approximations stored in .cmx files
|
|
|
|
contain the right names if the -for-pack option is active. *)
|
|
|
|
|
|
|
|
let getglobal id =
|
|
|
|
Uprim(Pgetglobal (Ident.create_persistent (Compilenv.symbol_for_global id)),
|
2007-01-29 04:11:18 -08:00
|
|
|
[], Debuginfo.none)
|
2006-01-04 08:55:50 -08:00
|
|
|
|
1995-07-11 11:03:29 -07:00
|
|
|
(* Check if a variable occurs in a [clambda] term. *)
|
|
|
|
|
|
|
|
let occurs_var var u =
|
|
|
|
let rec occurs = function
|
|
|
|
Uvar v -> v = var
|
2011-03-29 00:58:53 -07:00
|
|
|
| Uconst (cst,_) -> false
|
2007-01-29 04:11:18 -08:00
|
|
|
| Udirect_apply(lbl, args, _) -> List.exists occurs args
|
|
|
|
| Ugeneric_apply(funct, args, _) -> occurs funct || List.exists occurs args
|
1995-07-11 11:03:29 -07:00
|
|
|
| Uclosure(fundecls, clos) -> List.exists occurs clos
|
|
|
|
| Uoffset(u, ofs) -> occurs u
|
2000-12-28 05:07:42 -08:00
|
|
|
| Ulet(id, def, body) -> occurs def || occurs body
|
1995-07-11 11:03:29 -07:00
|
|
|
| Uletrec(decls, body) ->
|
2000-12-28 05:07:42 -08:00
|
|
|
List.exists (fun (id, u) -> occurs u) decls || occurs body
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(p, args, _) -> List.exists occurs args
|
1996-04-04 07:54:25 -08:00
|
|
|
| Uswitch(arg, s) ->
|
2001-02-19 12:15:42 -08:00
|
|
|
occurs arg ||
|
|
|
|
occurs_array s.us_actions_consts || occurs_array s.us_actions_blocks
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ustaticfail (_, args) -> List.exists occurs args
|
2000-12-28 05:07:42 -08:00
|
|
|
| Ucatch(_, _, body, hdlr) -> occurs body || occurs hdlr
|
|
|
|
| Utrywith(body, exn, hdlr) -> occurs body || occurs hdlr
|
1995-07-11 11:03:29 -07:00
|
|
|
| Uifthenelse(cond, ifso, ifnot) ->
|
2000-12-28 05:07:42 -08:00
|
|
|
occurs cond || occurs ifso || occurs ifnot
|
|
|
|
| Usequence(u1, u2) -> occurs u1 || occurs u2
|
|
|
|
| Uwhile(cond, body) -> occurs cond || occurs body
|
|
|
|
| Ufor(id, lo, hi, dir, body) -> occurs lo || occurs hi || occurs body
|
|
|
|
| Uassign(id, u) -> id = var || occurs u
|
2007-01-29 04:11:18 -08:00
|
|
|
| Usend(_, met, obj, args, _) ->
|
2000-12-28 05:07:42 -08:00
|
|
|
occurs met || occurs obj || List.exists occurs args
|
1995-07-11 11:03:29 -07:00
|
|
|
and occurs_array a =
|
|
|
|
try
|
|
|
|
for i = 0 to Array.length a - 1 do
|
|
|
|
if occurs a.(i) then raise Exit
|
|
|
|
done;
|
|
|
|
false
|
|
|
|
with Exit ->
|
|
|
|
true
|
|
|
|
in occurs u
|
|
|
|
|
1997-02-16 09:20:11 -08:00
|
|
|
(* Determine whether the estimated size of a clambda term is below
|
|
|
|
some threshold *)
|
|
|
|
|
|
|
|
let prim_size prim args =
|
|
|
|
match prim with
|
|
|
|
Pidentity -> 0
|
|
|
|
| Pgetglobal id -> 1
|
|
|
|
| Psetglobal id -> 1
|
|
|
|
| Pmakeblock(tag, mut) -> 5 + List.length args
|
|
|
|
| Pfield f -> 1
|
|
|
|
| Psetfield(f, isptr) -> if isptr then 4 else 1
|
|
|
|
| Pfloatfield f -> 1
|
|
|
|
| Psetfloatfield f -> 1
|
2007-02-09 05:31:15 -08:00
|
|
|
| Pduprecord _ -> 10 + List.length args
|
1997-02-16 09:20:11 -08:00
|
|
|
| Pccall p -> (if p.prim_alloc then 10 else 4) + List.length args
|
|
|
|
| Praise -> 4
|
|
|
|
| Pstringlength -> 5
|
|
|
|
| Pstringrefs | Pstringsets -> 6
|
|
|
|
| Pmakearray kind -> 5 + List.length args
|
|
|
|
| Parraylength kind -> if kind = Pgenarray then 6 else 2
|
|
|
|
| Parrayrefu kind -> if kind = Pgenarray then 12 else 2
|
|
|
|
| Parraysetu kind -> if kind = Pgenarray then 16 else 4
|
|
|
|
| Parrayrefs kind -> if kind = Pgenarray then 18 else 8
|
|
|
|
| Parraysets kind -> if kind = Pgenarray then 22 else 10
|
|
|
|
| Pbittest -> 3
|
2008-07-14 02:09:53 -07:00
|
|
|
| Pbigarrayref(_, ndims, _, _) -> 4 + ndims * 6
|
|
|
|
| Pbigarrayset(_, ndims, _, _) -> 4 + ndims * 6
|
1997-02-16 09:20:11 -08:00
|
|
|
| _ -> 2 (* arithmetic and comparisons *)
|
|
|
|
|
2001-02-19 12:15:42 -08:00
|
|
|
(* Very raw approximation of switch cost *)
|
2006-01-04 08:55:50 -08:00
|
|
|
|
1997-02-16 09:20:11 -08:00
|
|
|
let lambda_smaller lam threshold =
|
|
|
|
let size = ref 0 in
|
|
|
|
let rec lambda_size lam =
|
|
|
|
if !size > threshold then raise Exit;
|
|
|
|
match lam with
|
|
|
|
Uvar v -> ()
|
2011-03-29 00:58:53 -07:00
|
|
|
| Uconst(
|
2012-07-30 11:04:46 -07:00
|
|
|
(Const_base(Const_int _ | Const_char _ | Const_float _ |
|
2003-04-25 05:27:31 -07:00
|
|
|
Const_int32 _ | Const_int64 _ | Const_nativeint _) |
|
2011-03-29 00:58:53 -07:00
|
|
|
Const_pointer _), _) -> incr size
|
|
|
|
(* Structured Constants are now emitted during closure conversion. *)
|
|
|
|
| Uconst (_, Some _) -> incr size
|
1997-02-16 09:20:11 -08:00
|
|
|
| Uconst _ ->
|
|
|
|
raise Exit (* avoid duplication of structured constants *)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Udirect_apply(fn, args, _) ->
|
1997-02-16 09:20:11 -08:00
|
|
|
size := !size + 4; lambda_list_size args
|
2007-01-29 04:11:18 -08:00
|
|
|
| Ugeneric_apply(fn, args, _) ->
|
1997-02-16 09:20:11 -08:00
|
|
|
size := !size + 6; lambda_size fn; lambda_list_size args
|
|
|
|
| Uclosure(defs, vars) ->
|
|
|
|
raise Exit (* inlining would duplicate function definitions *)
|
|
|
|
| Uoffset(lam, ofs) ->
|
|
|
|
incr size; lambda_size lam
|
|
|
|
| Ulet(id, lam, body) ->
|
|
|
|
lambda_size lam; lambda_size body
|
|
|
|
| Uletrec(bindings, body) ->
|
|
|
|
raise Exit (* usually too large *)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(prim, args, _) ->
|
1997-02-16 09:20:11 -08:00
|
|
|
size := !size + prim_size prim args;
|
|
|
|
lambda_list_size args
|
|
|
|
| Uswitch(lam, cases) ->
|
2001-02-19 12:15:42 -08:00
|
|
|
if Array.length cases.us_actions_consts > 1 then size := !size + 5 ;
|
|
|
|
if Array.length cases.us_actions_blocks > 1 then size := !size + 5 ;
|
1997-02-16 09:20:11 -08:00
|
|
|
lambda_size lam;
|
2001-02-19 12:15:42 -08:00
|
|
|
lambda_array_size cases.us_actions_consts ;
|
|
|
|
lambda_array_size cases.us_actions_blocks
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ustaticfail (_,args) -> lambda_list_size args
|
|
|
|
| Ucatch(_, _, body, handler) ->
|
1997-02-16 09:20:11 -08:00
|
|
|
incr size; lambda_size body; lambda_size handler
|
|
|
|
| Utrywith(body, id, handler) ->
|
|
|
|
size := !size + 8; lambda_size body; lambda_size handler
|
|
|
|
| Uifthenelse(cond, ifso, ifnot) ->
|
|
|
|
size := !size + 2;
|
|
|
|
lambda_size cond; lambda_size ifso; lambda_size ifnot
|
|
|
|
| Usequence(lam1, lam2) ->
|
|
|
|
lambda_size lam1; lambda_size lam2
|
|
|
|
| Uwhile(cond, body) ->
|
|
|
|
size := !size + 2; lambda_size cond; lambda_size body
|
|
|
|
| Ufor(id, low, high, dir, body) ->
|
|
|
|
size := !size + 4; lambda_size low; lambda_size high; lambda_size body
|
|
|
|
| Uassign(id, lam) ->
|
|
|
|
incr size; lambda_size lam
|
2007-01-29 04:11:18 -08:00
|
|
|
| Usend(_, met, obj, args, _) ->
|
1997-02-16 09:20:11 -08:00
|
|
|
size := !size + 8;
|
|
|
|
lambda_size met; lambda_size obj; lambda_list_size args
|
|
|
|
and lambda_list_size l = List.iter lambda_size l
|
|
|
|
and lambda_array_size a = Array.iter lambda_size a in
|
|
|
|
try
|
|
|
|
lambda_size lam; !size <= threshold
|
|
|
|
with Exit ->
|
|
|
|
false
|
|
|
|
|
1999-10-14 10:07:59 -07:00
|
|
|
(* Check if a clambda term is ``pure'',
|
|
|
|
that is without side-effects *and* not containing function definitions *)
|
|
|
|
|
|
|
|
let rec is_pure_clambda = function
|
|
|
|
Uvar v -> true
|
2011-03-29 00:58:53 -07:00
|
|
|
| Uconst _ -> true
|
2007-02-09 05:31:15 -08:00
|
|
|
| Uprim((Psetglobal _ | Psetfield _ | Psetfloatfield _ | Pduprecord _ |
|
1999-10-14 10:07:59 -07:00
|
|
|
Pccall _ | Praise | Poffsetref _ | Pstringsetu | Pstringsets |
|
2007-01-29 04:11:18 -08:00
|
|
|
Parraysetu _ | Parraysets _ | Pbigarrayset _), _, _) -> false
|
|
|
|
| Uprim(p, args, _) -> List.for_all is_pure_clambda args
|
1999-10-14 10:07:59 -07:00
|
|
|
| _ -> false
|
|
|
|
|
1998-04-30 05:11:35 -07:00
|
|
|
(* Simplify primitive operations on integers *)
|
|
|
|
|
2011-03-29 00:58:53 -07:00
|
|
|
let make_const_int n = (Uconst(Const_base(Const_int n), None), Value_integer n)
|
|
|
|
let make_const_ptr n = (Uconst(Const_pointer n, None), Value_constptr n)
|
1999-01-29 00:50:24 -08:00
|
|
|
let make_const_bool b = make_const_ptr(if b then 1 else 0)
|
1998-04-30 05:11:35 -07:00
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
let simplif_prim_pure p (args, approxs) dbg =
|
1998-04-30 05:11:35 -07:00
|
|
|
match approxs with
|
|
|
|
[Value_integer x] ->
|
|
|
|
begin match p with
|
|
|
|
Pidentity -> make_const_int x
|
|
|
|
| Pnegint -> make_const_int (-x)
|
2012-11-29 01:55:00 -08:00
|
|
|
| Pbswap16 ->
|
|
|
|
make_const_int (((x land 0xff) lsl 8) lor
|
|
|
|
((x land 0xff00) lsr 8))
|
1998-04-30 05:11:35 -07:00
|
|
|
| Poffsetint y -> make_const_int (x + y)
|
2007-01-29 04:11:18 -08:00
|
|
|
| _ -> (Uprim(p, args, dbg), Value_unknown)
|
1998-04-30 05:11:35 -07:00
|
|
|
end
|
|
|
|
| [Value_integer x; Value_integer y] ->
|
|
|
|
begin match p with
|
|
|
|
Paddint -> make_const_int(x + y)
|
|
|
|
| Psubint -> make_const_int(x - y)
|
|
|
|
| Pmulint -> make_const_int(x * y)
|
|
|
|
| Pdivint when y <> 0 -> make_const_int(x / y)
|
|
|
|
| Pmodint when y <> 0 -> make_const_int(x mod y)
|
|
|
|
| Pandint -> make_const_int(x land y)
|
|
|
|
| Porint -> make_const_int(x lor y)
|
|
|
|
| Pxorint -> make_const_int(x lxor y)
|
|
|
|
| Plslint -> make_const_int(x lsl y)
|
|
|
|
| Plsrint -> make_const_int(x lsr y)
|
|
|
|
| Pasrint -> make_const_int(x asr y)
|
|
|
|
| Pintcomp cmp ->
|
|
|
|
let result = match cmp with
|
|
|
|
Ceq -> x = y
|
|
|
|
| Cneq -> x <> y
|
|
|
|
| Clt -> x < y
|
|
|
|
| Cgt -> x > y
|
|
|
|
| Cle -> x <= y
|
|
|
|
| Cge -> x >= y in
|
1999-01-29 00:50:24 -08:00
|
|
|
make_const_bool result
|
2007-01-29 04:11:18 -08:00
|
|
|
| _ -> (Uprim(p, args, dbg), Value_unknown)
|
1999-01-29 00:50:24 -08:00
|
|
|
end
|
|
|
|
| [Value_constptr x] ->
|
|
|
|
begin match p with
|
|
|
|
Pidentity -> make_const_ptr x
|
|
|
|
| Pnot -> make_const_bool(x = 0)
|
1999-12-06 08:59:24 -08:00
|
|
|
| Pisint -> make_const_bool true
|
2012-11-29 01:55:00 -08:00
|
|
|
| Pctconst c ->
|
|
|
|
begin
|
|
|
|
match c with
|
|
|
|
| Big_endian -> make_const_bool Arch.big_endian
|
|
|
|
| Word_size -> make_const_int (8*Arch.size_int)
|
|
|
|
| Ostype_unix -> make_const_bool (Sys.os_type = "Unix")
|
|
|
|
| Ostype_win32 -> make_const_bool (Sys.os_type = "Win32")
|
|
|
|
| Ostype_cygwin -> make_const_bool (Sys.os_type = "Cygwin")
|
|
|
|
end
|
2007-01-29 04:11:18 -08:00
|
|
|
| _ -> (Uprim(p, args, dbg), Value_unknown)
|
1999-01-29 00:50:24 -08:00
|
|
|
end
|
|
|
|
| [Value_constptr x; Value_constptr y] ->
|
|
|
|
begin match p with
|
|
|
|
Psequand -> make_const_bool(x <> 0 && y <> 0)
|
|
|
|
| Psequor -> make_const_bool(x <> 0 || y <> 0)
|
2007-01-29 04:11:18 -08:00
|
|
|
| _ -> (Uprim(p, args, dbg), Value_unknown)
|
1998-04-30 05:11:35 -07:00
|
|
|
end
|
|
|
|
| _ ->
|
2007-01-29 04:11:18 -08:00
|
|
|
(Uprim(p, args, dbg), Value_unknown)
|
1999-10-14 10:07:59 -07:00
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
let simplif_prim p (args, approxs as args_approxs) dbg =
|
1999-10-14 10:07:59 -07:00
|
|
|
if List.for_all is_pure_clambda args
|
2007-01-29 04:11:18 -08:00
|
|
|
then simplif_prim_pure p args_approxs dbg
|
|
|
|
else (Uprim(p, args, dbg), Value_unknown)
|
1999-10-14 10:07:59 -07:00
|
|
|
|
2000-09-01 06:27:11 -07:00
|
|
|
(* Substitute variables in a [ulambda] term (a body of an inlined function)
|
|
|
|
and perform some more simplifications on integer primitives.
|
|
|
|
Also perform alpha-conversion on let-bound identifiers to avoid
|
|
|
|
clashes with locally-generated identifiers.
|
1998-06-09 06:40:10 -07:00
|
|
|
The variables must not be assigned in the term.
|
|
|
|
This is used to substitute "trivial" arguments for parameters
|
2005-10-21 00:55:01 -07:00
|
|
|
during inline expansion, and also for the translation of let rec
|
|
|
|
over functions. *)
|
1998-06-09 06:40:10 -07:00
|
|
|
|
|
|
|
let approx_ulam = function
|
2011-03-29 00:58:53 -07:00
|
|
|
Uconst(Const_base(Const_int n),_) -> Value_integer n
|
|
|
|
| Uconst(Const_base(Const_char c),_) -> Value_integer(Char.code c)
|
|
|
|
| Uconst(Const_pointer n,_) -> Value_constptr n
|
1998-06-09 06:40:10 -07:00
|
|
|
| _ -> Value_unknown
|
|
|
|
|
2000-09-01 06:27:11 -07:00
|
|
|
let rec substitute sb ulam =
|
|
|
|
match ulam with
|
|
|
|
Uvar v ->
|
|
|
|
begin try Tbl.find v sb with Not_found -> ulam end
|
2011-03-29 00:58:53 -07:00
|
|
|
| Uconst _ -> ulam
|
2007-01-29 04:11:18 -08:00
|
|
|
| Udirect_apply(lbl, args, dbg) ->
|
|
|
|
Udirect_apply(lbl, List.map (substitute sb) args, dbg)
|
|
|
|
| Ugeneric_apply(fn, args, dbg) ->
|
|
|
|
Ugeneric_apply(substitute sb fn, List.map (substitute sb) args, dbg)
|
2000-09-01 06:27:11 -07:00
|
|
|
| Uclosure(defs, env) ->
|
2005-10-21 00:55:01 -07:00
|
|
|
(* Question: should we rename function labels as well? Otherwise,
|
|
|
|
there is a risk that function labels are not globally unique.
|
|
|
|
This should not happen in the current system because:
|
|
|
|
- Inlined function bodies contain no Uclosure nodes
|
|
|
|
(cf. function [lambda_smaller])
|
|
|
|
- When we substitute offsets for idents bound by let rec
|
|
|
|
in [close], case [Lletrec], we discard the original
|
|
|
|
let rec body and use only the substituted term. *)
|
|
|
|
Uclosure(defs, List.map (substitute sb) env)
|
2000-09-01 06:27:11 -07:00
|
|
|
| Uoffset(u, ofs) -> Uoffset(substitute sb u, ofs)
|
|
|
|
| Ulet(id, u1, u2) ->
|
|
|
|
let id' = Ident.rename id in
|
|
|
|
Ulet(id', substitute sb u1, substitute (Tbl.add id (Uvar id') sb) u2)
|
|
|
|
| Uletrec(bindings, body) ->
|
2005-10-21 00:55:01 -07:00
|
|
|
let bindings1 =
|
|
|
|
List.map (fun (id, rhs) -> (id, Ident.rename id, rhs)) bindings in
|
|
|
|
let sb' =
|
2006-01-04 08:55:50 -08:00
|
|
|
List.fold_right
|
2005-10-21 00:55:01 -07:00
|
|
|
(fun (id, id', _) s -> Tbl.add id (Uvar id') s)
|
2005-10-24 02:05:27 -07:00
|
|
|
bindings1 sb in
|
2005-10-21 00:55:01 -07:00
|
|
|
Uletrec(
|
|
|
|
List.map (fun (id, id', rhs) -> (id', substitute sb' rhs)) bindings1,
|
|
|
|
substitute sb' body)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim(p, args, dbg) ->
|
2000-09-01 06:27:11 -07:00
|
|
|
let sargs = List.map (substitute sb) args in
|
2007-01-29 04:11:18 -08:00
|
|
|
let (res, _) = simplif_prim p (sargs, List.map approx_ulam sargs) dbg in
|
2000-09-01 06:27:11 -07:00
|
|
|
res
|
|
|
|
| Uswitch(arg, sw) ->
|
|
|
|
Uswitch(substitute sb arg,
|
|
|
|
{ sw with
|
2001-02-19 12:15:42 -08:00
|
|
|
us_actions_consts =
|
|
|
|
Array.map (substitute sb) sw.us_actions_consts;
|
|
|
|
us_actions_blocks =
|
|
|
|
Array.map (substitute sb) sw.us_actions_blocks;
|
2000-09-01 06:27:11 -07:00
|
|
|
})
|
2001-02-05 00:46:34 -08:00
|
|
|
| Ustaticfail (nfail, args) ->
|
|
|
|
Ustaticfail (nfail, List.map (substitute sb) args)
|
|
|
|
| Ucatch(nfail, ids, u1, u2) ->
|
|
|
|
Ucatch(nfail, ids, substitute sb u1, substitute sb u2)
|
2000-09-01 06:27:11 -07:00
|
|
|
| Utrywith(u1, id, u2) ->
|
|
|
|
let id' = Ident.rename id in
|
|
|
|
Utrywith(substitute sb u1, id', substitute (Tbl.add id (Uvar id') sb) u2)
|
|
|
|
| Uifthenelse(u1, u2, u3) ->
|
|
|
|
begin match substitute sb u1 with
|
2011-03-29 00:58:53 -07:00
|
|
|
Uconst(Const_pointer n, _) ->
|
2000-09-01 06:27:11 -07:00
|
|
|
if n <> 0 then substitute sb u2 else substitute sb u3
|
|
|
|
| su1 ->
|
|
|
|
Uifthenelse(su1, substitute sb u2, substitute sb u3)
|
|
|
|
end
|
|
|
|
| Usequence(u1, u2) -> Usequence(substitute sb u1, substitute sb u2)
|
|
|
|
| Uwhile(u1, u2) -> Uwhile(substitute sb u1, substitute sb u2)
|
|
|
|
| Ufor(id, u1, u2, dir, u3) ->
|
|
|
|
let id' = Ident.rename id in
|
|
|
|
Ufor(id', substitute sb u1, substitute sb u2, dir,
|
|
|
|
substitute (Tbl.add id (Uvar id') sb) u3)
|
|
|
|
| Uassign(id, u) ->
|
|
|
|
let id' =
|
|
|
|
try
|
|
|
|
match Tbl.find id sb with Uvar i -> i | _ -> assert false
|
|
|
|
with Not_found ->
|
|
|
|
id in
|
|
|
|
Uassign(id', substitute sb u)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Usend(k, u1, u2, ul, dbg) ->
|
2013-03-09 14:38:52 -08:00
|
|
|
Usend(k, substitute sb u1, substitute sb u2, List.map (substitute sb) ul,
|
|
|
|
dbg)
|
1998-06-09 06:40:10 -07:00
|
|
|
|
|
|
|
(* Perform an inline expansion *)
|
|
|
|
|
|
|
|
let is_simple_argument = function
|
|
|
|
Uvar _ -> true
|
2003-04-25 05:27:31 -07:00
|
|
|
| Uconst(Const_base(Const_int _ | Const_char _ | Const_float _ |
|
2011-03-29 00:58:53 -07:00
|
|
|
Const_int32 _ | Const_int64 _ | Const_nativeint _),_) ->
|
2003-04-25 05:27:31 -07:00
|
|
|
true
|
2011-03-29 00:58:53 -07:00
|
|
|
| Uconst(Const_pointer _, _) -> true
|
1998-06-09 06:40:10 -07:00
|
|
|
| _ -> false
|
|
|
|
|
1999-03-04 01:49:06 -08:00
|
|
|
let no_effects = function
|
|
|
|
Uclosure _ -> true
|
2011-03-29 00:58:53 -07:00
|
|
|
| Uconst(Const_base(Const_string _),_) -> true
|
1999-03-04 01:49:06 -08:00
|
|
|
| u -> is_simple_argument u
|
|
|
|
|
2004-11-03 01:23:56 -08:00
|
|
|
let rec bind_params_rec subst params args body =
|
1998-06-09 06:40:10 -07:00
|
|
|
match (params, args) with
|
|
|
|
([], []) -> substitute subst body
|
|
|
|
| (p1 :: pl, a1 :: al) ->
|
1999-03-04 01:49:06 -08:00
|
|
|
if is_simple_argument a1 then
|
2004-11-03 01:23:56 -08:00
|
|
|
bind_params_rec (Tbl.add p1 a1 subst) pl al body
|
1999-03-04 01:49:06 -08:00
|
|
|
else begin
|
2001-02-05 00:46:34 -08:00
|
|
|
let p1' = Ident.rename p1 in
|
2004-11-03 01:23:56 -08:00
|
|
|
let body' =
|
|
|
|
bind_params_rec (Tbl.add p1 (Uvar p1') subst) pl al body in
|
2001-02-05 00:46:34 -08:00
|
|
|
if occurs_var p1 body then Ulet(p1', a1, body')
|
1999-03-04 01:49:06 -08:00
|
|
|
else if no_effects a1 then body'
|
|
|
|
else Usequence(a1, body')
|
|
|
|
end
|
1998-06-09 06:40:10 -07:00
|
|
|
| (_, _) -> assert false
|
|
|
|
|
2004-11-03 01:23:56 -08:00
|
|
|
let bind_params params args body =
|
|
|
|
(* Reverse parameters and arguments to preserve right-to-left
|
|
|
|
evaluation order (PR#2910). *)
|
|
|
|
bind_params_rec Tbl.empty (List.rev params) (List.rev args) body
|
|
|
|
|
1998-11-11 01:38:22 -08:00
|
|
|
(* Check if a lambda term is ``pure'',
|
1998-06-09 06:40:10 -07:00
|
|
|
that is without side-effects *and* not containing function definitions *)
|
|
|
|
|
|
|
|
let rec is_pure = function
|
|
|
|
Lvar v -> true
|
1998-11-11 01:38:22 -08:00
|
|
|
| Lconst cst -> true
|
2007-02-09 05:31:15 -08:00
|
|
|
| Lprim((Psetglobal _ | Psetfield _ | Psetfloatfield _ | Pduprecord _ |
|
1998-11-11 01:38:22 -08:00
|
|
|
Pccall _ | Praise | Poffsetref _ | Pstringsetu | Pstringsets |
|
2008-03-18 07:25:40 -07:00
|
|
|
Parraysetu _ | Parraysets _ | Pbigarrayset _), _) -> false
|
1998-11-11 01:38:22 -08:00
|
|
|
| Lprim(p, args) -> List.for_all is_pure args
|
2007-01-29 04:11:18 -08:00
|
|
|
| Levent(lam, ev) -> is_pure lam
|
1998-06-09 06:40:10 -07:00
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
(* Generate a direct application *)
|
|
|
|
|
|
|
|
let direct_apply fundesc funct ufunct uargs =
|
|
|
|
let app_args =
|
|
|
|
if fundesc.fun_closed then uargs else uargs @ [ufunct] in
|
|
|
|
let app =
|
|
|
|
match fundesc.fun_inline with
|
2007-01-29 04:11:18 -08:00
|
|
|
None -> Udirect_apply(fundesc.fun_label, app_args, Debuginfo.none)
|
2004-11-03 01:23:56 -08:00
|
|
|
| Some(params, body) -> bind_params params app_args body in
|
1998-06-09 06:40:10 -07:00
|
|
|
(* If ufunct can contain side-effects or function definitions,
|
|
|
|
we must make sure that it is evaluated exactly once.
|
|
|
|
If the function is not closed, we evaluate ufunct as part of the
|
|
|
|
arguments.
|
|
|
|
If the function is closed, we force the evaluation of ufunct first. *)
|
|
|
|
if not fundesc.fun_closed || is_pure funct
|
|
|
|
then app
|
|
|
|
else Usequence(ufunct, app)
|
|
|
|
|
1999-01-29 00:50:24 -08:00
|
|
|
(* Add [Value_integer] or [Value_constptr] info to the approximation
|
|
|
|
of an application *)
|
1998-06-09 06:40:10 -07:00
|
|
|
|
|
|
|
let strengthen_approx appl approx =
|
|
|
|
match approx_ulam appl with
|
1999-01-29 00:50:24 -08:00
|
|
|
(Value_integer _ | Value_constptr _) as intapprox -> intapprox
|
1998-06-09 06:40:10 -07:00
|
|
|
| _ -> approx
|
|
|
|
|
1999-01-29 00:50:24 -08:00
|
|
|
(* If a term has approximation Value_integer or Value_constptr and is pure,
|
|
|
|
replace it by an integer constant *)
|
1998-11-11 01:38:22 -08:00
|
|
|
|
|
|
|
let check_constant_result lam ulam approx =
|
|
|
|
match approx with
|
|
|
|
Value_integer n when is_pure lam -> make_const_int n
|
1999-01-29 00:50:24 -08:00
|
|
|
| Value_constptr n when is_pure lam -> make_const_ptr n
|
1998-11-11 01:38:22 -08:00
|
|
|
| _ -> (ulam, approx)
|
|
|
|
|
1999-01-29 00:50:24 -08:00
|
|
|
(* Evaluate an expression with known value for its side effects only,
|
|
|
|
or discard it if it's pure *)
|
|
|
|
|
|
|
|
let sequence_constant_expr lam ulam1 (ulam2, approx2 as res2) =
|
|
|
|
if is_pure lam then res2 else (Usequence(ulam1, ulam2), approx2)
|
|
|
|
|
1996-02-18 06:42:27 -08:00
|
|
|
(* Maintain the approximation of the global structure being defined *)
|
|
|
|
|
|
|
|
let global_approx = ref([||] : value_approximation array)
|
|
|
|
|
2000-09-01 06:27:11 -07:00
|
|
|
(* Maintain the nesting depth for functions *)
|
|
|
|
|
|
|
|
let function_nesting_depth = ref 0
|
|
|
|
let excessive_function_nesting_depth = 5
|
|
|
|
|
2007-01-29 04:11:18 -08:00
|
|
|
(* Decorate clambda term with debug information *)
|
|
|
|
|
|
|
|
let rec add_debug_info ev u =
|
|
|
|
match ev.lev_kind with
|
|
|
|
| Lev_after _ ->
|
|
|
|
begin match u with
|
|
|
|
| Udirect_apply(lbl, args, dinfo) ->
|
|
|
|
Udirect_apply(lbl, args, Debuginfo.from_call ev)
|
|
|
|
| Ugeneric_apply(Udirect_apply(lbl, args1, dinfo1),
|
|
|
|
args2, dinfo2) ->
|
|
|
|
Ugeneric_apply(Udirect_apply(lbl, args1, Debuginfo.from_call ev),
|
|
|
|
args2, Debuginfo.from_call ev)
|
|
|
|
| Ugeneric_apply(fn, args, dinfo) ->
|
|
|
|
Ugeneric_apply(fn, args, Debuginfo.from_call ev)
|
|
|
|
| Uprim(Praise, args, dinfo) ->
|
|
|
|
Uprim(Praise, args, Debuginfo.from_call ev)
|
|
|
|
| Uprim(p, args, dinfo) ->
|
|
|
|
Uprim(p, args, Debuginfo.from_call ev)
|
|
|
|
| Usend(kind, u1, u2, args, dinfo) ->
|
|
|
|
Usend(kind, u1, u2, args, Debuginfo.from_call ev)
|
|
|
|
| Usequence(u1, u2) ->
|
|
|
|
Usequence(u1, add_debug_info ev u2)
|
|
|
|
| _ -> u
|
|
|
|
end
|
|
|
|
| _ -> u
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Uncurry an expression and explicitate closures.
|
|
|
|
Also return the approximation of the expression.
|
|
|
|
The approximation environment [fenv] maps idents to approximations.
|
|
|
|
Idents not bound in [fenv] approximate to [Value_unknown].
|
|
|
|
The closure environment [cenv] maps idents to [ulambda] terms.
|
|
|
|
It is used to substitute environment accesses for free identifiers. *)
|
|
|
|
|
1998-04-30 05:11:35 -07:00
|
|
|
let close_approx_var fenv cenv id =
|
|
|
|
let approx = try Tbl.find id fenv with Not_found -> Value_unknown in
|
|
|
|
match approx with
|
|
|
|
Value_integer n ->
|
|
|
|
make_const_int n
|
1999-01-29 00:50:24 -08:00
|
|
|
| Value_constptr n ->
|
|
|
|
make_const_ptr n
|
1998-04-30 05:11:35 -07:00
|
|
|
| approx ->
|
|
|
|
let subst = try Tbl.find id cenv with Not_found -> Uvar id in
|
|
|
|
(subst, approx)
|
1995-07-07 05:07:07 -07:00
|
|
|
|
1998-04-30 05:11:35 -07:00
|
|
|
let close_var fenv cenv id =
|
|
|
|
let (ulam, app) = close_approx_var fenv cenv id in ulam
|
1995-07-07 05:07:07 -07:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
let rec close fenv cenv = function
|
|
|
|
Lvar id ->
|
1998-04-30 05:11:35 -07:00
|
|
|
close_approx_var fenv cenv id
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lconst cst ->
|
1998-04-30 05:11:35 -07:00
|
|
|
begin match cst with
|
2011-03-29 00:58:53 -07:00
|
|
|
Const_base(Const_int n) -> (Uconst (cst,None), Value_integer n)
|
2013-03-09 14:38:52 -08:00
|
|
|
| Const_base(Const_char c) -> (Uconst (cst,None),
|
|
|
|
Value_integer(Char.code c))
|
2011-03-29 00:58:53 -07:00
|
|
|
| Const_pointer n -> (Uconst (cst, None), Value_constptr n)
|
2013-03-09 14:38:52 -08:00
|
|
|
| _ -> (Uconst (cst, Some (Compilenv.new_structured_constant cst true)),
|
|
|
|
Value_unknown)
|
1998-04-30 05:11:35 -07:00
|
|
|
end
|
1996-10-22 06:36:12 -07:00
|
|
|
| Lfunction(kind, params, body) as funct ->
|
1996-04-22 04:15:41 -07:00
|
|
|
close_one_function fenv cenv (Ident.create "fun") funct
|
2011-06-12 03:04:46 -07:00
|
|
|
|
2012-07-30 11:04:46 -07:00
|
|
|
(* We convert [f a] to [let a' = a in fun b c -> f a' b c]
|
2011-06-12 03:04:46 -07:00
|
|
|
when fun_arity > nargs *)
|
2007-05-16 01:21:41 -07:00
|
|
|
| Lapply(funct, args, loc) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let nargs = List.length args in
|
1996-10-22 06:36:12 -07:00
|
|
|
begin match (close fenv cenv funct, close_list fenv cenv args) with
|
|
|
|
((ufunct, Value_closure(fundesc, approx_res)),
|
2007-01-29 04:11:18 -08:00
|
|
|
[Uprim(Pmakeblock(_, _), uargs, _)])
|
1996-10-22 06:36:12 -07:00
|
|
|
when List.length uargs = - fundesc.fun_arity ->
|
1998-06-09 06:40:10 -07:00
|
|
|
let app = direct_apply fundesc funct ufunct uargs in
|
|
|
|
(app, strengthen_approx app approx_res)
|
1996-10-22 06:36:12 -07:00
|
|
|
| ((ufunct, Value_closure(fundesc, approx_res)), uargs)
|
1995-07-02 09:41:48 -07:00
|
|
|
when nargs = fundesc.fun_arity ->
|
1998-06-09 06:40:10 -07:00
|
|
|
let app = direct_apply fundesc funct ufunct uargs in
|
|
|
|
(app, strengthen_approx app approx_res)
|
2011-06-12 03:04:46 -07:00
|
|
|
|
|
|
|
| ((ufunct, Value_closure(fundesc, approx_res)), uargs)
|
|
|
|
when nargs < fundesc.fun_arity ->
|
2012-07-30 11:04:46 -07:00
|
|
|
let first_args = List.map (fun arg ->
|
|
|
|
(Ident.create "arg", arg) ) uargs in
|
2013-03-09 14:38:52 -08:00
|
|
|
let final_args =
|
|
|
|
Array.to_list (Array.init (fundesc.fun_arity - nargs)
|
|
|
|
(fun _ -> Ident.create "arg")) in
|
2012-07-30 11:04:46 -07:00
|
|
|
let rec iter args body =
|
|
|
|
match args with
|
|
|
|
[] -> body
|
|
|
|
| (arg1, arg2) :: args ->
|
|
|
|
iter args
|
|
|
|
(Ulet ( arg1, arg2, body))
|
|
|
|
in
|
|
|
|
let internal_args =
|
|
|
|
(List.map (fun (arg1, arg2) -> Lvar arg1) first_args)
|
|
|
|
@ (List.map (fun arg -> Lvar arg ) final_args)
|
|
|
|
in
|
|
|
|
let (new_fun, approx) = close fenv cenv
|
|
|
|
(Lfunction(
|
|
|
|
Curried, final_args, Lapply(funct, internal_args, loc)))
|
|
|
|
in
|
|
|
|
let new_fun = iter first_args new_fun in
|
|
|
|
(new_fun, approx)
|
2011-06-12 03:04:46 -07:00
|
|
|
|
1996-10-22 06:36:12 -07:00
|
|
|
| ((ufunct, Value_closure(fundesc, approx_res)), uargs)
|
|
|
|
when fundesc.fun_arity > 0 && nargs > fundesc.fun_arity ->
|
|
|
|
let (first_args, rem_args) = split_list fundesc.fun_arity uargs in
|
|
|
|
(Ugeneric_apply(direct_apply fundesc funct ufunct first_args,
|
2007-01-29 04:11:18 -08:00
|
|
|
rem_args, Debuginfo.none),
|
1995-07-02 09:41:48 -07:00
|
|
|
Value_unknown)
|
1996-10-22 06:36:12 -07:00
|
|
|
| ((ufunct, _), uargs) ->
|
2007-01-29 04:11:18 -08:00
|
|
|
(Ugeneric_apply(ufunct, uargs, Debuginfo.none), Value_unknown)
|
1995-07-02 09:41:48 -07:00
|
|
|
end
|
2011-07-20 02:17:07 -07:00
|
|
|
| Lsend(kind, met, obj, args, _) ->
|
1996-04-22 04:15:41 -07:00
|
|
|
let (umet, _) = close fenv cenv met in
|
|
|
|
let (uobj, _) = close fenv cenv obj in
|
2007-01-29 04:11:18 -08:00
|
|
|
(Usend(kind, umet, uobj, close_list fenv cenv args, Debuginfo.none),
|
|
|
|
Value_unknown)
|
1995-12-15 01:55:46 -08:00
|
|
|
| Llet(str, id, lam, body) ->
|
1995-07-07 05:07:07 -07:00
|
|
|
let (ulam, alam) = close_named fenv cenv id lam in
|
1998-04-30 05:11:35 -07:00
|
|
|
begin match (str, alam) with
|
|
|
|
(Variable, _) ->
|
|
|
|
let (ubody, abody) = close fenv cenv body in
|
|
|
|
(Ulet(id, ulam, ubody), abody)
|
1999-01-29 00:50:24 -08:00
|
|
|
| (_, (Value_integer _ | Value_constptr _))
|
|
|
|
when str = Alias || is_pure lam ->
|
1998-04-30 05:11:35 -07:00
|
|
|
close (Tbl.add id alam fenv) cenv body
|
|
|
|
| (_, _) ->
|
|
|
|
let (ubody, abody) = close (Tbl.add id alam fenv) cenv body in
|
|
|
|
(Ulet(id, ulam, ubody), abody)
|
|
|
|
end
|
1995-07-07 05:07:07 -07:00
|
|
|
| Lletrec(defs, body) ->
|
|
|
|
if List.for_all
|
1996-10-22 06:36:12 -07:00
|
|
|
(function (id, Lfunction(_, _, _)) -> true | _ -> false)
|
1995-07-07 05:07:07 -07:00
|
|
|
defs
|
|
|
|
then begin
|
|
|
|
(* Simple case: only function definitions *)
|
|
|
|
let (clos, infos) = close_functions fenv cenv defs in
|
1996-04-22 04:15:41 -07:00
|
|
|
let clos_ident = Ident.create "clos" in
|
1995-07-07 05:07:07 -07:00
|
|
|
let fenv_body =
|
|
|
|
List.fold_right
|
|
|
|
(fun (id, pos, approx) fenv -> Tbl.add id approx fenv)
|
|
|
|
infos fenv in
|
1995-07-17 09:10:15 -07:00
|
|
|
let (ubody, approx) = close fenv_body cenv body in
|
2005-10-21 00:55:01 -07:00
|
|
|
let sb =
|
|
|
|
List.fold_right
|
|
|
|
(fun (id, pos, approx) sb ->
|
|
|
|
Tbl.add id (Uoffset(Uvar clos_ident, pos)) sb)
|
|
|
|
infos Tbl.empty in
|
|
|
|
(Ulet(clos_ident, clos, substitute sb ubody),
|
1995-07-17 09:10:15 -07:00
|
|
|
approx)
|
1995-07-07 05:07:07 -07:00
|
|
|
end else begin
|
|
|
|
(* General case: recursive definition of values *)
|
|
|
|
let rec clos_defs = function
|
|
|
|
[] -> ([], fenv)
|
|
|
|
| (id, lam) :: rem ->
|
|
|
|
let (udefs, fenv_body) = clos_defs rem in
|
|
|
|
let (ulam, approx) = close fenv cenv lam in
|
|
|
|
((id, ulam) :: udefs, Tbl.add id approx fenv_body) in
|
|
|
|
let (udefs, fenv_body) = clos_defs defs in
|
|
|
|
let (ubody, approx) = close fenv_body cenv body in
|
|
|
|
(Uletrec(udefs, ubody), approx)
|
|
|
|
end
|
2012-01-23 06:49:39 -08:00
|
|
|
| Lprim(Pdirapply loc,[funct;arg])
|
|
|
|
| Lprim(Prevapply loc,[arg;funct]) ->
|
|
|
|
close fenv cenv (Lapply(funct, [arg], loc))
|
1998-11-11 01:38:22 -08:00
|
|
|
| Lprim(Pgetglobal id, []) as lam ->
|
|
|
|
check_constant_result lam
|
2006-01-04 08:55:50 -08:00
|
|
|
(getglobal id)
|
|
|
|
(Compilenv.global_approx id)
|
1995-11-09 05:21:49 -08:00
|
|
|
| Lprim(Pmakeblock(tag, mut) as prim, lams) ->
|
1995-07-02 09:41:48 -07:00
|
|
|
let (ulams, approxs) = List.split (List.map (close fenv cenv) lams) in
|
2007-01-29 04:11:18 -08:00
|
|
|
(Uprim(prim, ulams, Debuginfo.none),
|
1995-11-09 05:21:49 -08:00
|
|
|
begin match mut with
|
|
|
|
Immutable -> Value_tuple(Array.of_list approxs)
|
|
|
|
| Mutable -> Value_unknown
|
|
|
|
end)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lprim(Pfield n, [lam]) ->
|
|
|
|
let (ulam, approx) = close fenv cenv lam in
|
1998-04-30 05:11:35 -07:00
|
|
|
let fieldapprox =
|
|
|
|
match approx with
|
|
|
|
Value_tuple a when n < Array.length a -> a.(n)
|
|
|
|
| _ -> Value_unknown in
|
2013-03-09 14:38:52 -08:00
|
|
|
check_constant_result lam (Uprim(Pfield n, [ulam], Debuginfo.none))
|
|
|
|
fieldapprox
|
1996-02-18 06:42:27 -08:00
|
|
|
| Lprim(Psetfield(n, _), [Lprim(Pgetglobal id, []); lam]) ->
|
|
|
|
let (ulam, approx) = close fenv cenv lam in
|
|
|
|
(!global_approx).(n) <- approx;
|
2007-01-29 04:11:18 -08:00
|
|
|
(Uprim(Psetfield(n, false), [getglobal id; ulam], Debuginfo.none),
|
|
|
|
Value_unknown)
|
|
|
|
| Lprim(Praise, [Levent(arg, ev)]) ->
|
|
|
|
let (ulam, approx) = close fenv cenv arg in
|
|
|
|
(Uprim(Praise, [ulam], Debuginfo.from_raise ev),
|
1996-02-18 06:42:27 -08:00
|
|
|
Value_unknown)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lprim(p, args) ->
|
2007-01-29 04:11:18 -08:00
|
|
|
simplif_prim p (close_list_approx fenv cenv args) Debuginfo.none
|
2004-11-22 00:42:34 -08:00
|
|
|
| Lswitch(arg, sw) ->
|
2001-02-19 12:15:42 -08:00
|
|
|
(* NB: failaction might get copied, thus it should be some Lstaticraise *)
|
1995-07-02 09:41:48 -07:00
|
|
|
let (uarg, _) = close fenv cenv arg in
|
2001-02-19 12:15:42 -08:00
|
|
|
let const_index, const_actions =
|
|
|
|
close_switch fenv cenv sw.sw_consts sw.sw_numconsts sw.sw_failaction
|
|
|
|
and block_index, block_actions =
|
|
|
|
close_switch fenv cenv sw.sw_blocks sw.sw_numblocks sw.sw_failaction in
|
2006-01-04 08:55:50 -08:00
|
|
|
(Uswitch(uarg,
|
1996-04-04 07:54:25 -08:00
|
|
|
{us_index_consts = const_index;
|
2001-02-19 12:15:42 -08:00
|
|
|
us_actions_consts = const_actions;
|
1996-04-04 07:54:25 -08:00
|
|
|
us_index_blocks = block_index;
|
2001-02-19 12:15:42 -08:00
|
|
|
us_actions_blocks = block_actions}),
|
1995-07-02 09:41:48 -07:00
|
|
|
Value_unknown)
|
2000-10-02 07:08:30 -07:00
|
|
|
| Lstaticraise (i, args) ->
|
|
|
|
(Ustaticfail (i, close_list fenv cenv args), Value_unknown)
|
|
|
|
| Lstaticcatch(body, (i, vars), handler) ->
|
2000-08-11 12:50:59 -07:00
|
|
|
let (ubody, _) = close fenv cenv body in
|
|
|
|
let (uhandler, _) = close fenv cenv handler in
|
2000-10-02 07:08:30 -07:00
|
|
|
(Ucatch(i, vars, ubody, uhandler), Value_unknown)
|
1995-07-02 09:41:48 -07:00
|
|
|
| Ltrywith(body, id, handler) ->
|
|
|
|
let (ubody, _) = close fenv cenv body in
|
|
|
|
let (uhandler, _) = close fenv cenv handler in
|
|
|
|
(Utrywith(ubody, id, uhandler), Value_unknown)
|
|
|
|
| Lifthenelse(arg, ifso, ifnot) ->
|
1999-01-29 00:50:24 -08:00
|
|
|
begin match close fenv cenv arg with
|
|
|
|
(uarg, Value_constptr n) ->
|
|
|
|
sequence_constant_expr arg uarg
|
|
|
|
(close fenv cenv (if n = 0 then ifnot else ifso))
|
2006-01-04 08:55:50 -08:00
|
|
|
| (uarg, _ ) ->
|
1999-01-29 00:50:24 -08:00
|
|
|
let (uifso, _) = close fenv cenv ifso in
|
|
|
|
let (uifnot, _) = close fenv cenv ifnot in
|
|
|
|
(Uifthenelse(uarg, uifso, uifnot), Value_unknown)
|
|
|
|
end
|
1995-07-02 09:41:48 -07:00
|
|
|
| Lsequence(lam1, lam2) ->
|
|
|
|
let (ulam1, _) = close fenv cenv lam1 in
|
|
|
|
let (ulam2, approx) = close fenv cenv lam2 in
|
|
|
|
(Usequence(ulam1, ulam2), approx)
|
|
|
|
| Lwhile(cond, body) ->
|
|
|
|
let (ucond, _) = close fenv cenv cond in
|
|
|
|
let (ubody, _) = close fenv cenv body in
|
|
|
|
(Uwhile(ucond, ubody), Value_unknown)
|
|
|
|
| Lfor(id, lo, hi, dir, body) ->
|
|
|
|
let (ulo, _) = close fenv cenv lo in
|
|
|
|
let (uhi, _) = close fenv cenv hi in
|
|
|
|
let (ubody, _) = close fenv cenv body in
|
|
|
|
(Ufor(id, ulo, uhi, dir, ubody), Value_unknown)
|
1995-11-25 07:38:43 -08:00
|
|
|
| Lassign(id, lam) ->
|
|
|
|
let (ulam, _) = close fenv cenv lam in
|
|
|
|
(Uassign(id, ulam), Value_unknown)
|
2007-01-29 04:11:18 -08:00
|
|
|
| Levent(lam, ev) ->
|
|
|
|
let (ulam, approx) = close fenv cenv lam in
|
|
|
|
(add_debug_info ev ulam, approx)
|
|
|
|
| Lifused _ ->
|
|
|
|
assert false
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
and close_list fenv cenv = function
|
|
|
|
[] -> []
|
|
|
|
| lam :: rem ->
|
|
|
|
let (ulam, _) = close fenv cenv lam in
|
|
|
|
ulam :: close_list fenv cenv rem
|
|
|
|
|
1998-04-30 05:11:35 -07:00
|
|
|
and close_list_approx fenv cenv = function
|
|
|
|
[] -> ([], [])
|
|
|
|
| lam :: rem ->
|
|
|
|
let (ulam, approx) = close fenv cenv lam in
|
|
|
|
let (ulams, approxs) = close_list_approx fenv cenv rem in
|
|
|
|
(ulam :: ulams, approx :: approxs)
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
and close_named fenv cenv id = function
|
1996-10-22 06:36:12 -07:00
|
|
|
Lfunction(kind, params, body) as funct ->
|
1995-07-07 05:07:07 -07:00
|
|
|
close_one_function fenv cenv id funct
|
1995-07-02 09:41:48 -07:00
|
|
|
| lam ->
|
|
|
|
close fenv cenv lam
|
|
|
|
|
1995-07-07 05:07:07 -07:00
|
|
|
(* Build a shared closure for a set of mutually recursive functions *)
|
|
|
|
|
|
|
|
and close_functions fenv cenv fun_defs =
|
2000-09-01 06:27:11 -07:00
|
|
|
(* Update and check nesting depth *)
|
|
|
|
incr function_nesting_depth;
|
|
|
|
let initially_closed =
|
|
|
|
!function_nesting_depth < excessive_function_nesting_depth in
|
1995-07-07 05:07:07 -07:00
|
|
|
(* Determine the free variables of the functions *)
|
|
|
|
let fv =
|
|
|
|
IdentSet.elements (free_variables (Lletrec(fun_defs, lambda_unit))) in
|
1995-12-19 02:18:05 -08:00
|
|
|
(* Build the function descriptors for the functions.
|
|
|
|
Initially all functions are assumed not to need their environment
|
1995-07-11 11:03:29 -07:00
|
|
|
parameter. *)
|
1995-07-07 05:07:07 -07:00
|
|
|
let uncurried_defs =
|
|
|
|
List.map
|
1995-12-19 02:18:05 -08:00
|
|
|
(function
|
2004-11-22 00:42:34 -08:00
|
|
|
(id, Lfunction(kind, params, body)) ->
|
2004-01-05 12:26:19 -08:00
|
|
|
let label = Compilenv.make_symbol (Some (Ident.unique_name id)) in
|
1996-10-22 06:36:12 -07:00
|
|
|
let arity = List.length params in
|
1995-12-19 02:18:05 -08:00
|
|
|
let fundesc =
|
|
|
|
{fun_label = label;
|
1996-10-22 06:36:12 -07:00
|
|
|
fun_arity = (if kind = Tupled then -arity else arity);
|
2000-09-01 06:27:11 -07:00
|
|
|
fun_closed = initially_closed;
|
1997-02-16 09:20:11 -08:00
|
|
|
fun_inline = None } in
|
1995-12-19 02:18:05 -08:00
|
|
|
(id, params, body, fundesc)
|
|
|
|
| (_, _) -> fatal_error "Closure.close_functions")
|
1995-07-07 05:07:07 -07:00
|
|
|
fun_defs in
|
|
|
|
(* Build an approximate fenv for compiling the functions *)
|
|
|
|
let fenv_rec =
|
|
|
|
List.fold_right
|
|
|
|
(fun (id, params, body, fundesc) fenv ->
|
|
|
|
Tbl.add id (Value_closure(fundesc, Value_unknown)) fenv)
|
|
|
|
uncurried_defs fenv in
|
|
|
|
(* Determine the offsets of each function's closure in the shared block *)
|
|
|
|
let env_pos = ref (-1) in
|
|
|
|
let clos_offsets =
|
|
|
|
List.map
|
|
|
|
(fun (id, params, body, fundesc) ->
|
|
|
|
let pos = !env_pos + 1 in
|
1996-10-22 06:36:12 -07:00
|
|
|
env_pos := !env_pos + 1 + (if fundesc.fun_arity <> 1 then 3 else 2);
|
1995-07-07 05:07:07 -07:00
|
|
|
pos)
|
|
|
|
uncurried_defs in
|
|
|
|
let fv_pos = !env_pos in
|
1995-07-11 11:03:29 -07:00
|
|
|
(* This reference will be set to false if the hypothesis that a function
|
|
|
|
does not use its environment parameter is invalidated. *)
|
2000-09-01 06:27:11 -07:00
|
|
|
let useless_env = ref initially_closed in
|
1995-07-07 05:07:07 -07:00
|
|
|
(* Translate each function definition *)
|
|
|
|
let clos_fundef (id, params, body, fundesc) env_pos =
|
2012-02-21 09:41:02 -08:00
|
|
|
let dbg = match body with
|
|
|
|
| Levent (_,({lev_kind=Lev_function} as ev)) -> Debuginfo.from_call ev
|
|
|
|
| _ -> Debuginfo.none in
|
1996-04-22 04:15:41 -07:00
|
|
|
let env_param = Ident.create "env" in
|
1995-07-07 05:07:07 -07:00
|
|
|
let cenv_fv =
|
|
|
|
build_closure_env env_param (fv_pos - env_pos) fv in
|
|
|
|
let cenv_body =
|
|
|
|
List.fold_right2
|
|
|
|
(fun (id, params, arity, body) pos env ->
|
|
|
|
Tbl.add id (Uoffset(Uvar env_param, pos - env_pos)) env)
|
|
|
|
uncurried_defs clos_offsets cenv_fv in
|
|
|
|
let (ubody, approx) = close fenv_rec cenv_body body in
|
2000-09-01 06:27:11 -07:00
|
|
|
if !useless_env && occurs_var env_param ubody then useless_env := false;
|
1995-07-13 10:16:20 -07:00
|
|
|
let fun_params = if !useless_env then params else params @ [env_param] in
|
2012-02-21 09:41:02 -08:00
|
|
|
({ label = fundesc.fun_label;
|
|
|
|
arity = fundesc.fun_arity;
|
|
|
|
params = fun_params;
|
|
|
|
body = ubody;
|
|
|
|
dbg },
|
1995-07-07 05:07:07 -07:00
|
|
|
(id, env_pos, Value_closure(fundesc, approx))) in
|
1995-07-11 11:03:29 -07:00
|
|
|
(* Translate all function definitions. *)
|
2000-09-01 06:27:11 -07:00
|
|
|
let clos_info_list =
|
|
|
|
if initially_closed then begin
|
|
|
|
let cl = List.map2 clos_fundef uncurried_defs clos_offsets in
|
|
|
|
(* If the hypothesis that the environment parameters are useless has been
|
|
|
|
invalidated, then set [fun_closed] to false in all descriptions and
|
|
|
|
recompile *)
|
|
|
|
if !useless_env then cl else begin
|
|
|
|
List.iter
|
|
|
|
(fun (id, params, body, fundesc) -> fundesc.fun_closed <- false)
|
|
|
|
uncurried_defs;
|
|
|
|
List.map2 clos_fundef uncurried_defs clos_offsets
|
|
|
|
end
|
|
|
|
end else
|
|
|
|
(* Excessive closure nesting: assume environment parameter is used *)
|
|
|
|
List.map2 clos_fundef uncurried_defs clos_offsets
|
|
|
|
in
|
|
|
|
(* Update nesting depth *)
|
|
|
|
decr function_nesting_depth;
|
1995-07-11 11:03:29 -07:00
|
|
|
(* Return the Uclosure node and the list of all identifiers defined,
|
|
|
|
with offsets and approximations. *)
|
|
|
|
let (clos, infos) = List.split clos_info_list in
|
1998-04-30 05:11:35 -07:00
|
|
|
(Uclosure(clos, List.map (close_var fenv cenv) fv), infos)
|
1995-07-07 05:07:07 -07:00
|
|
|
|
1997-02-16 09:20:11 -08:00
|
|
|
(* Same, for one non-recursive function *)
|
1995-07-07 05:07:07 -07:00
|
|
|
|
|
|
|
and close_one_function fenv cenv id funct =
|
|
|
|
match close_functions fenv cenv [id, funct] with
|
2012-02-21 09:41:02 -08:00
|
|
|
((Uclosure([f], _) as clos),
|
1997-02-16 09:20:11 -08:00
|
|
|
[_, _, (Value_closure(fundesc, _) as approx)]) ->
|
|
|
|
(* See if the function can be inlined *)
|
2012-02-21 09:41:02 -08:00
|
|
|
if lambda_smaller f.body
|
|
|
|
(!Clflags.inline_threshold + List.length f.params)
|
|
|
|
then fundesc.fun_inline <- Some(f.params, f.body);
|
1997-02-16 09:20:11 -08:00
|
|
|
(clos, approx)
|
1995-07-07 05:07:07 -07:00
|
|
|
| _ -> fatal_error "Closure.close_one_function"
|
1995-07-02 09:41:48 -07:00
|
|
|
|
1996-04-04 07:54:25 -08:00
|
|
|
(* Close a switch *)
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2001-02-19 12:15:42 -08:00
|
|
|
and close_switch fenv cenv cases num_keys default =
|
|
|
|
let index = Array.create num_keys 0
|
2008-08-01 05:52:14 -07:00
|
|
|
and store = mk_store Lambda.same in
|
2001-02-19 12:15:42 -08:00
|
|
|
|
|
|
|
(* First default case *)
|
|
|
|
begin match default with
|
|
|
|
| Some def when List.length cases < num_keys ->
|
|
|
|
ignore (store.act_store def)
|
|
|
|
| _ -> ()
|
|
|
|
end ;
|
|
|
|
(* Then all other cases *)
|
1995-07-02 09:41:48 -07:00
|
|
|
List.iter
|
2001-02-19 12:15:42 -08:00
|
|
|
(fun (key,lam) ->
|
|
|
|
index.(key) <- store.act_store lam)
|
|
|
|
cases ;
|
|
|
|
(* Compile action *)
|
|
|
|
let actions =
|
|
|
|
Array.map
|
|
|
|
(fun lam ->
|
|
|
|
let ulam,_ = close fenv cenv lam in
|
|
|
|
ulam)
|
|
|
|
(store.act_get ()) in
|
|
|
|
match actions with
|
|
|
|
| [| |] -> [| |], [| |] (* May happen when default is None *)
|
|
|
|
| _ -> index, actions
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* The entry point *)
|
|
|
|
|
1996-02-18 06:42:27 -08:00
|
|
|
let intro size lam =
|
2000-09-01 06:27:11 -07:00
|
|
|
function_nesting_depth := 0;
|
1996-04-22 04:15:41 -07:00
|
|
|
global_approx := Array.create size Value_unknown;
|
1996-02-18 06:42:27 -08:00
|
|
|
Compilenv.set_global_approx(Value_tuple !global_approx);
|
1999-02-04 02:31:16 -08:00
|
|
|
let (ulam, approx) = close Tbl.empty Tbl.empty lam in
|
1996-02-18 06:42:27 -08:00
|
|
|
global_approx := [||];
|
|
|
|
ulam
|