1996-04-22 04:15:41 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1996-04-22 04:15:41 -07:00
|
|
|
(* *)
|
|
|
|
(* Jerome Vouillon, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
|
|
|
(* 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. *)
|
1996-04-22 04:15:41 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
|
|
|
open Misc
|
2004-05-26 04:10:52 -07:00
|
|
|
open Primitive
|
1996-04-22 04:15:41 -07:00
|
|
|
open Asttypes
|
|
|
|
open Longident
|
|
|
|
open Lambda
|
|
|
|
|
|
|
|
(* Get oo primitives identifiers *)
|
|
|
|
|
|
|
|
let oo_prim name =
|
|
|
|
try
|
|
|
|
transl_path
|
2002-04-24 02:49:06 -07:00
|
|
|
(fst (Env.lookup_value (Ldot (Lident "CamlinternalOO", name)) Env.empty))
|
1996-04-22 04:15:41 -07:00
|
|
|
with Not_found ->
|
|
|
|
fatal_error ("Primitive " ^ name ^ " not found.")
|
|
|
|
|
2003-11-25 01:20:45 -08:00
|
|
|
(* Share blocks *)
|
|
|
|
|
|
|
|
let consts : (structured_constant, Ident.t) Hashtbl.t = Hashtbl.create 17
|
|
|
|
|
|
|
|
let share c =
|
|
|
|
match c with
|
|
|
|
Const_block (n, l) when l <> [] ->
|
|
|
|
begin try
|
|
|
|
Lvar (Hashtbl.find consts c)
|
|
|
|
with Not_found ->
|
|
|
|
let id = Ident.create "shared" in
|
|
|
|
Hashtbl.add consts c id;
|
|
|
|
Lvar id
|
|
|
|
end
|
|
|
|
| _ -> Lconst c
|
|
|
|
|
1996-04-22 04:15:41 -07:00
|
|
|
(* Collect labels *)
|
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
let cache_required = ref false
|
|
|
|
let method_cache = ref lambda_unit
|
|
|
|
let method_count = ref 0
|
|
|
|
let method_table = ref []
|
|
|
|
|
|
|
|
let meth_tag s = Lconst(Const_base(Const_int(Btype.hash_variant s)))
|
|
|
|
|
|
|
|
let next_cache tag =
|
|
|
|
let n = !method_count in
|
|
|
|
incr method_count;
|
|
|
|
(tag, [!method_cache; Lconst(Const_base(Const_int n))])
|
|
|
|
|
|
|
|
let rec is_path = function
|
|
|
|
Lvar _ | Lprim (Pgetglobal _, []) | Lconst _ -> true
|
|
|
|
| Lprim (Pfield _, [lam]) -> is_path lam
|
|
|
|
| Lprim ((Parrayrefu _ | Parrayrefs _), [lam1; lam2]) ->
|
|
|
|
is_path lam1 && is_path lam2
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let meth obj lab =
|
|
|
|
let tag = meth_tag lab in
|
|
|
|
if not (!cache_required && !Clflags.native_code) then (tag, []) else
|
|
|
|
if not (is_path obj) then next_cache tag else
|
1996-04-22 04:15:41 -07:00
|
|
|
try
|
2004-05-26 04:10:52 -07:00
|
|
|
let r = List.assoc obj !method_table in
|
|
|
|
try
|
|
|
|
(tag, List.assoc tag !r)
|
|
|
|
with Not_found ->
|
|
|
|
let p = next_cache tag in
|
|
|
|
r := p :: !r;
|
|
|
|
p
|
1996-04-22 04:15:41 -07:00
|
|
|
with Not_found ->
|
2004-05-26 04:10:52 -07:00
|
|
|
let p = next_cache tag in
|
|
|
|
method_table := (obj, ref [p]) :: !method_table;
|
|
|
|
p
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
let reset_labels () =
|
2003-11-25 01:20:45 -08:00
|
|
|
Hashtbl.clear consts;
|
2004-05-26 04:10:52 -07:00
|
|
|
method_count := 0;
|
|
|
|
method_table := []
|
1996-04-22 04:15:41 -07:00
|
|
|
|
|
|
|
(* Insert labels *)
|
|
|
|
|
|
|
|
let string s = Lconst (Const_base (Const_string s))
|
2004-05-26 04:10:52 -07:00
|
|
|
let int n = Lconst (Const_base (Const_int n))
|
|
|
|
|
|
|
|
let prim_makearray =
|
|
|
|
{ prim_name = "caml_make_vect"; prim_arity = 2; prim_alloc = true;
|
|
|
|
prim_native_name = ""; prim_native_float = false }
|
1996-04-22 04:15:41 -07:00
|
|
|
|
1997-03-12 02:30:19 -08:00
|
|
|
let transl_label_init expr =
|
2003-11-25 01:20:45 -08:00
|
|
|
let expr =
|
|
|
|
Hashtbl.fold
|
|
|
|
(fun c id expr -> Llet(Alias, id, Lconst c, expr))
|
|
|
|
consts expr
|
|
|
|
in
|
|
|
|
reset_labels ();
|
|
|
|
expr
|
|
|
|
|
2004-05-26 04:10:52 -07:00
|
|
|
let transl_store_label_init glob size f arg =
|
|
|
|
method_cache := Lprim(Pfield size, [Lprim(Pgetglobal glob, [])]);
|
|
|
|
let expr = f arg in
|
|
|
|
let (size, expr) =
|
|
|
|
if !method_count = 0 then (size, expr) else
|
|
|
|
(size+1,
|
|
|
|
Lsequence(
|
|
|
|
Lprim(Psetfield(size, false),
|
2010-01-22 04:48:24 -08:00
|
|
|
[Lprim(Pgetglobal glob, []);
|
|
|
|
Lprim (Pccall prim_makearray, [int !method_count; int 0])]),
|
2004-05-26 04:10:52 -07:00
|
|
|
expr))
|
|
|
|
in
|
|
|
|
(size, transl_label_init expr)
|
2003-11-25 01:20:45 -08:00
|
|
|
|
|
|
|
(* Share classes *)
|
|
|
|
|
|
|
|
let wrapping = ref false
|
|
|
|
let top_env = ref Env.empty
|
|
|
|
let classes = ref []
|
2008-10-08 06:09:39 -07:00
|
|
|
let method_ids = ref IdentSet.empty
|
2003-11-25 01:20:45 -08:00
|
|
|
|
|
|
|
let oo_add_class id =
|
|
|
|
classes := id :: !classes;
|
2004-05-26 04:10:52 -07:00
|
|
|
(!top_env, !cache_required)
|
2003-11-25 01:20:45 -08:00
|
|
|
|
|
|
|
let oo_wrap env req f x =
|
|
|
|
if !wrapping then
|
2004-05-26 04:10:52 -07:00
|
|
|
if !cache_required then f x else
|
|
|
|
try cache_required := true; let lam = f x in cache_required := false; lam
|
|
|
|
with exn -> cache_required := false; raise exn
|
2003-11-25 01:20:45 -08:00
|
|
|
else try
|
|
|
|
wrapping := true;
|
2004-05-26 04:10:52 -07:00
|
|
|
cache_required := req;
|
2003-11-25 01:20:45 -08:00
|
|
|
top_env := env;
|
|
|
|
classes := [];
|
2008-10-08 06:09:39 -07:00
|
|
|
method_ids := IdentSet.empty;
|
2003-11-25 01:20:45 -08:00
|
|
|
let lambda = f x in
|
|
|
|
let lambda =
|
|
|
|
List.fold_left
|
|
|
|
(fun lambda id ->
|
|
|
|
Llet(StrictOpt, id,
|
|
|
|
Lprim(Pmakeblock(0, Mutable),
|
|
|
|
[lambda_unit; lambda_unit; lambda_unit]),
|
|
|
|
lambda))
|
|
|
|
lambda !classes
|
1997-03-12 02:30:19 -08:00
|
|
|
in
|
2003-11-25 01:20:45 -08:00
|
|
|
wrapping := false;
|
|
|
|
top_env := Env.empty;
|
|
|
|
lambda
|
|
|
|
with exn ->
|
|
|
|
wrapping := false;
|
|
|
|
top_env := Env.empty;
|
|
|
|
raise exn
|