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
|
|
|
(* A variant of the "lambda" code with direct / indirect calls explicit
|
|
|
|
and closures explicit too *)
|
|
|
|
|
|
|
|
open Asttypes
|
|
|
|
open Lambda
|
|
|
|
|
|
|
|
type function_label = string
|
|
|
|
|
2014-03-06 09:03:16 -08:00
|
|
|
type ustructured_constant =
|
2014-04-25 01:41:13 -07:00
|
|
|
| Uconst_float of float
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_int32 of int32
|
|
|
|
| Uconst_int64 of int64
|
|
|
|
| Uconst_nativeint of nativeint
|
|
|
|
| Uconst_block of int * uconstant list
|
2014-04-25 01:41:13 -07:00
|
|
|
| Uconst_float_array of float list
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst_string of string
|
|
|
|
|
|
|
|
and uconstant =
|
|
|
|
| Uconst_ref of string * ustructured_constant
|
|
|
|
| Uconst_int of int
|
|
|
|
| Uconst_ptr of int
|
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
type ulambda =
|
|
|
|
Uvar of Ident.t
|
2014-03-06 09:03:16 -08:00
|
|
|
| Uconst of uconstant
|
2007-01-29 04:11:18 -08:00
|
|
|
| Udirect_apply of function_label * ulambda list * Debuginfo.t
|
|
|
|
| Ugeneric_apply of ulambda * ulambda list * Debuginfo.t
|
2012-02-21 09:41:02 -08:00
|
|
|
| Uclosure of ufunction list * ulambda list
|
1995-07-07 05:07:07 -07:00
|
|
|
| Uoffset of ulambda * int
|
1995-07-02 09:41:48 -07:00
|
|
|
| Ulet of Ident.t * ulambda * ulambda
|
|
|
|
| Uletrec of (Ident.t * ulambda) list * ulambda
|
2007-01-29 04:11:18 -08:00
|
|
|
| Uprim of primitive * ulambda list * Debuginfo.t
|
1996-04-04 07:54:25 -08:00
|
|
|
| Uswitch of ulambda * ulambda_switch
|
2014-04-07 08:43:20 -07:00
|
|
|
| Ustringswitch of ulambda * (string * ulambda) list * ulambda option
|
2000-10-02 07:08:30 -07:00
|
|
|
| Ustaticfail of int * ulambda list
|
|
|
|
| Ucatch of int * Ident.t list * ulambda * ulambda
|
1995-07-02 09:41:48 -07:00
|
|
|
| Utrywith of ulambda * Ident.t * ulambda
|
|
|
|
| Uifthenelse of ulambda * ulambda * ulambda
|
|
|
|
| Usequence of ulambda * ulambda
|
|
|
|
| Uwhile of ulambda * ulambda
|
|
|
|
| Ufor of Ident.t * ulambda * ulambda * direction_flag * ulambda
|
1995-11-25 07:38:43 -08:00
|
|
|
| Uassign of Ident.t * ulambda
|
2007-01-29 04:11:18 -08:00
|
|
|
| Usend of meth_kind * ulambda * ulambda * ulambda list * Debuginfo.t
|
1995-07-02 09:41:48 -07:00
|
|
|
|
2012-02-21 09:41:02 -08:00
|
|
|
and ufunction = {
|
|
|
|
label : function_label;
|
|
|
|
arity : int;
|
|
|
|
params : Ident.t list;
|
|
|
|
body : ulambda;
|
|
|
|
dbg : Debuginfo.t
|
|
|
|
}
|
|
|
|
|
1996-04-04 07:54:25 -08:00
|
|
|
and ulambda_switch =
|
|
|
|
{ us_index_consts: int array;
|
2001-02-19 12:15:42 -08:00
|
|
|
us_actions_consts : ulambda array;
|
1996-04-04 07:54:25 -08:00
|
|
|
us_index_blocks: int array;
|
2001-02-19 12:15:42 -08:00
|
|
|
us_actions_blocks: ulambda array}
|
1996-04-04 07:54:25 -08:00
|
|
|
|
1995-07-02 09:41:48 -07:00
|
|
|
(* Description of known functions *)
|
|
|
|
|
|
|
|
type function_description =
|
|
|
|
{ fun_label: function_label; (* Label of direct entry point *)
|
|
|
|
fun_arity: int; (* Number of arguments *)
|
1997-02-16 09:20:11 -08:00
|
|
|
mutable fun_closed: bool; (* True if environment not used *)
|
2014-04-25 01:41:13 -07:00
|
|
|
mutable fun_inline: (Ident.t list * ulambda) option;
|
|
|
|
mutable fun_float_const_prop: bool (* Can propagate FP consts *)
|
|
|
|
}
|
1995-07-02 09:41:48 -07:00
|
|
|
|
|
|
|
(* Approximation of values *)
|
|
|
|
|
|
|
|
type value_approximation =
|
|
|
|
Value_closure of function_description * value_approximation
|
|
|
|
| Value_tuple of value_approximation array
|
|
|
|
| Value_unknown
|
2014-03-06 09:03:16 -08:00
|
|
|
| Value_const of uconstant
|
2014-03-10 03:02:31 -07:00
|
|
|
| Value_global_field of string * int
|
2014-05-25 09:46:23 -07:00
|
|
|
|
|
|
|
(* Comparison functions for constants. We must not use Pervasives.compare
|
|
|
|
because it compares "0.0" and "-0.0" equal. (PR#6442) *)
|
|
|
|
|
|
|
|
let compare_floats x1 x2 =
|
|
|
|
Int64.compare (Int64.bits_of_float x1) (Int64.bits_of_float x2)
|
|
|
|
|
|
|
|
let rec compare_float_lists l1 l2 =
|
|
|
|
match l1, l2 with
|
|
|
|
| [], [] -> 0
|
|
|
|
| [], _::_ -> -1
|
|
|
|
| _::_, [] -> 1
|
|
|
|
| h1::t1, h2::t2 ->
|
|
|
|
let c = compare_floats h1 h2 in
|
|
|
|
if c <> 0 then c else compare_float_lists t1 t2
|
|
|
|
|
|
|
|
let compare_constants c1 c2 =
|
|
|
|
match c1, c2 with
|
|
|
|
| Uconst_ref(lbl1, c1), Uconst_ref(lbl2, c2) -> String.compare lbl1 lbl2
|
|
|
|
(* Same labels -> same constants.
|
|
|
|
Different labels -> different constants, even if the contents
|
|
|
|
match, because of string constants that must not be
|
|
|
|
reshared. *)
|
|
|
|
| Uconst_int n1, Uconst_int n2 -> Pervasives.compare n1 n2
|
|
|
|
| Uconst_ptr n1, Uconst_ptr n2 -> Pervasives.compare n1 n2
|
|
|
|
| Uconst_ref _, _ -> -1
|
|
|
|
| Uconst_int _, Uconst_ref _ -> 1
|
|
|
|
| Uconst_int _, Uconst_ptr _ -> -1
|
|
|
|
| Uconst_ptr _, _ -> 1
|
|
|
|
|
|
|
|
let rec compare_constant_lists l1 l2 =
|
|
|
|
match l1, l2 with
|
|
|
|
| [], [] -> 0
|
|
|
|
| [], _::_ -> -1
|
|
|
|
| _::_, [] -> 1
|
|
|
|
| h1::t1, h2::t2 ->
|
|
|
|
let c = compare_constants h1 h2 in
|
|
|
|
if c <> 0 then c else compare_constant_lists t1 t2
|
|
|
|
|
|
|
|
let rank_structured_constant = function
|
|
|
|
| Uconst_float _ -> 0
|
|
|
|
| Uconst_int32 _ -> 1
|
|
|
|
| Uconst_int64 _ -> 2
|
|
|
|
| Uconst_nativeint _ -> 3
|
|
|
|
| Uconst_block _ -> 4
|
|
|
|
| Uconst_float_array _ -> 5
|
|
|
|
| Uconst_string _ -> 6
|
|
|
|
|
|
|
|
let compare_structured_constants c1 c2 =
|
|
|
|
match c1, c2 with
|
|
|
|
| Uconst_float x1, Uconst_float x2 -> compare_floats x1 x2
|
|
|
|
| Uconst_int32 x1, Uconst_int32 x2 -> Int32.compare x1 x2
|
|
|
|
| Uconst_int64 x1, Uconst_int64 x2 -> Int64.compare x1 x2
|
|
|
|
| Uconst_nativeint x1, Uconst_nativeint x2 -> Nativeint.compare x1 x2
|
|
|
|
| Uconst_block(t1, l1), Uconst_block(t2, l2) ->
|
|
|
|
let c = t1 - t2 (* no overflow possible here *) in
|
|
|
|
if c <> 0 then c else compare_constant_lists l1 l2
|
|
|
|
| Uconst_float_array l1, Uconst_float_array l2 ->
|
|
|
|
compare_float_lists l1 l2
|
|
|
|
| Uconst_string s1, Uconst_string s2 -> String.compare s1 s2
|
|
|
|
| _, _ -> rank_structured_constant c1 - rank_structured_constant c2
|
|
|
|
(* no overflow possible here *)
|